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(); } } }
运行:
(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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2022-02-12 Python2/Python3使用virtualenv创建虚拟环境venv