主要是参考
http://docs.oracle.com/javaee/6/api/javax/mail/Session.html#getInstance(java.util.Properties)
https://www.tutorialspoint.com/javamail_api/javamail_api_authentication.htm
https://www.javatpoint.com/example-of-sending-email-using-java-mail-api
package com.xmal.dms.util; import java.util.*; import javax.mail.*; import javax.mail.internet.*; import org.apache.commons.lang.StringUtils; import org.springframework.mail.javamail.MimeMessageHelper; import javax.activation.*; public class MailUtils { public static void sendMail(List<String> recipientsList,String content,String Subject) throws AddressException { // Recipient's email ID needs to be mentioned. InternetAddress[] recipientsArr = new InternetAddress[recipientsList.size()]; recipientsArr = InternetAddress.parse(StringUtils.join(recipientsList,",")); // Sender's email ID needs to be mentioned String user = "myzonedj@163.com"; String passWord = "adventure/2011/"; // Assuming you are sending email from localhost String host = "smtp.163.com"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); //setup password properties.setProperty("mail.smtp.auth", "true"); // Get the default Session object. Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user,passWord); } }); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); MimeMessageHelper messageHelper = new MimeMessageHelper(message,"UTF-8"); // Set To: header field of the header. messageHelper.setTo(recipientsArr); // Set From: header field of the header. messageHelper.setFrom(new InternetAddress(user));
// Set Subject: header field messageHelper.setSubject(Subject); // Send the actual HTML message, as big as you like //message.setContent(content, "text/html"); messageHelper.setText(content, true); // message.setText(content); // Send message Transport.send(messageHelper.getMimeMessage());; System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }