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告警测试");
}
上一篇:基于python发送邮件(写成了一个类)


下一篇:哪个企业邮箱比较好