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 @   船长博客  阅读(286)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-02-12 Python2/Python3使用virtualenv创建虚拟环境venv
永远相信美好的事情即将发生!
点击右上角即可分享
微信分享提示