Java教程

java发送邮件

本文主要是介绍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发送邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!