Java发送QQ邮件
- jar包链接
链接:https://pan.baidu.com/s/1mt7LsPVIuMl5HLXEOppWkQ
提取码:x4jf
- 示例代码:
package java2;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 测试发送邮件
*/
public class SendEmail {
public static void main(String[] args) throws Exception {
Properties pro = new Properties();
pro.setProperty("mail.host","smtp.qq.com");//设置QQ邮件服务器
pro.setProperty("mail.transport.protocol","smtp");//设置传输协议
pro.setProperty("mail.smtp.auth","true");//需要验证用户名密码
//Java发送邮件的5个步骤
//1.创建应用程序所需的环境信息的Session对象
Session session = Session.getDefaultInstance(pro, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","授权码");//发件人邮箱及授权码
}
});
session.setDebug(true);//开启debug模式,查看进度状态
//2.通过session得到transport对象
Transport ts = session.getTransport();
//3.使用邮箱用户名及授权码连接上邮件服务器
ts.connect("smtp.qq.com","[email protected]","授权码");
//4.创建邮件
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指定邮件发件人
message.setFrom(new InternetAddress("[email protected]"));
//指定邮件收件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));
//邮件标题
message.setSubject("这是一封测试邮件");
//邮件的文本内容
message.setContent("你好啊","text/html;charset=UTF-8");
//发送邮件
ts.sendMessage(message,message.getAllRecipients());
ts.close();
}
}
参考链接:https://blog.csdn.net/qq_41151659/article/details/96475739
手敲一遍,涨涨姿势~
原文地址:https://www.cnblogs.com/qujialin/p/12150767.html
时间: 2024-10-19 04:44:07