java发送邮件

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了java发送邮件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.5.0-b01</version>
</dependency>
public static void sendJMToolMail(String subject,String text) throws Exception {
	String myEmailAccount = "252xxxxx@qq.com"; // 发件人邮箱地址
	String myEmailPassword = "swmecxxxxxxrbjfd"; // 发件人邮箱密码
	String myEmailSMTPHost = "smtp.qq.com"; // 发件人邮箱服务器协议
	String senderPersonalName = "JMTool上线告警助手"; // 发件人昵称
	String receiveMailAccount = "ivan.xxxx.com"; //收件人邮箱数组
	String mailTransportProtocol = "smtp"; // 使用的协议(JavaMail规范要求)
	Properties props = new Properties();
	props.setProperty("mail.transport.protocol", mailTransportProtocol);
	props.setProperty("mail.smtp.host", myEmailSMTPHost);   // 发件人的邮箱的 SMTP 服务器地址
	props.setProperty("mail.smtp.auth", "true");            // 需要请求认证
	Session session = Session.getInstance(props);
	session.setDebug(false);
	MimeMessage message = new MimeMessage(session);
	message.setFrom(new InternetAddress(myEmailAccount, senderPersonalName, "UTF-8"));
	Address[] internetAddressTo = InternetAddress.parse(receiveMailAccount);
	message.setRecipients(MimeMessage.RecipientType.TO, internetAddressTo);
	message.setSubject(subject, "UTF-8");
	message.setContent(text, "text/html;charset=UTF-8");
	message.setSentDate(new Date());
	message.saveChanges();
	Transport transport = session.getTransport();
	transport.connect(myEmailAccount, myEmailPassword);
	transport.sendMessage(message, message.getAllRecipients());
	transport.close();
}
public static void main(String[] args) throws Exception {
	sendJMToolMail("JMTOOL主题","JMTOOL告警测试");
}

脚本宝典总结

以上是脚本宝典为你收集整理的java发送邮件全部内容,希望文章能够帮你解决java发送邮件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: