Java发送QQ邮件附mail.jar和activation.jar包

111111是QQ邮箱账户,6666ufrtmxibjgf是QQ邮箱授权码,以下代码保存为hello.java

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class hello {
    public static void main(String[] args) {
        String to = "111111qq.com";
        String from = "111111@qq.com";
        String host = "smtp.qq.com";
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host",host);
        properties.put("mail.smtp.auth","true");


        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication()
            {
          //账户,邮箱授权码
return new PasswordAuthentication("111111","6666ufrtmxibjgf"); } }); try { MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(from)); mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); mimeMessage.setSubject("主题:黑客笔记"); mimeMessage.setText("正文:我是ailx10,我喜欢2进制安全和渗透技术"); Transport.send(mimeMessage); System.out.println("发送邮件成功"); } catch (MessagingException e) { e.printStackTrace(); } } }

运行:

下载两个jar包下载链接

(base) ➜  java javac -cp mail.jar:activation.jar hello.java 
(base) ➜  java java -cp .:mail.jar:activation.jar hello     
发送邮件成功

参考:
上述代码发送的是文本,发HTML和带附件参考:https://zhuanlan.zhihu.com/p/91153215
https://blog.csdn.net/sdrfghb/article/details/126845550

posted @ 2023-02-12 11:22  船长博客  阅读(260)  评论(0编辑  收藏  举报
永远相信美好的事情即将发生!