maven 导包:
<!-- email start -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- email end -->
代码:
public static void main(String[] args) throws MessagingException
{
Properties properties = new Properties();
// 连接协议
properties.put("mail.transport.protocol", "smtp");
// 主机名
properties.put("mail.smtp.host", "smtp.qq.com");
// 端口号
properties.put("mail.smtp.port", 465);
properties.put("mail.smtp.auth", "true");
// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.smtp.ssl.enable", "true");
// 设置是否显示debug信息 true 会在控制台显示相关信息
properties.put("mail.debug", "true");
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress("xxxx"));
// 设置收件人邮箱地址
//message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com"),new InternetAddress("xxx@qq.com")});
//一个收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com"));
// 设置邮件标题
message.setSubject("xxx");
// 设置邮件内容
message.setText("xxxx");
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
transport.connect("xxx", "xxxx");
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
java-发送邮件
最新推荐文章于 2021-02-27 12:26:06 发布