1.jar包依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
2.sendMailUtil类
public class SendMailUtil {
private static String mailFrom = "";
private static String mailPort = "465";
private static String mailHost = "smtp.263.net";
private static String mailUsername = "";//发件人的邮箱
private static String mailPassword = "";//发件人密码认证(qq邮箱、163等需要授权码)
public Authenticator createAuthenticator() {
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人邮箱账号
//String userId = props.getProperty("userId", "jiangmo01@outlook.com");
//发件人邮箱密码(qq、163等邮箱用的是授权码,outlook是密码)
//String password = props.getProperty("password", "Shiyueshiriqing");
return new PasswordAuthentication(mailUsername, mailPassword);
}
};
return authenticator;
}
public Properties createProperties() {
Properties props = new Properties();
//发件人
props.getProperty("fromEmail", mailFrom);
props.put("mail.smtp.port", mailPort);
props.put("mail.smtp.host", mailHost);
//当前smtp host设为可信任 否则抛出javax.mail.MessagingException: Could not convert socket to TLS
props.put("mail.smtp.ssl.trust", mailHost);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl", "true");
//开启debug调试,控制台会打印相关信息
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.fallback", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
return props;
}
public boolean sendOutlookMail(String receiver, String subject, String content) {
Properties props = createProperties();
//收件人
props.getProperty("toEmail", receiver);//对收件人重新赋值了
Session session = Session.getInstance(props, createAuthenticator());
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailUsername));
List list = new ArrayList();//不能使⽤string类型的类型,这样只能发送⼀个收件⼈
String []median=receiver.split(",");//对输⼊的多个邮件进⾏逗号分割
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
// InternetAddress[] address =(InternetAddress[])list.toArray(new InternetAddress[list.size()]);
// InternetAddress[] address = InternetAddress.parse(list.toString());
InternetAddress[] address = InternetAddress.parse(receiver);
message.setRecipients(Message.RecipientType.TO, address);
//标题
message.setSubject(subject);
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
//String emailText = getHtmlContent(sendEmailApi.getTemplateContent(tempValue),tempMap);
//文本类型正文
//mimeBodyPart.setText("尊敬的张三:\r您好!\r特邀您...");
/*String emailText = "<h3>这是一个大标题</h3>\n" +
"<br/>\n" +
"<p>写点什么好呢</p>";*/
//html类型正文
mimeBodyPart.setContent(content, "text/html;charset=UTF-8");
//添加正文
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
message.setSentDate(new Date());
message.saveChanges();
Transport.send(message);
System.out.println("邮件发送结束");
return true;
} catch (MessagingException me) {
System.out.println("错误信息:" + me.getMessage());
return false;
} catch (Exception ex) {
System.out.println("错误信息:" + ex.getMessage());
return false;
}
}
}
3.测试邮件发送
public static void main(String[] args) {
try {
SendMailUtil mail = new SendMailUtil();
boolean bool = mail.sendOutlookMail("**@126.com", "测试邮件发送"
, "您好,这是一封测试邮件,请忽略");
} catch (Exception e) {
System.out.println("发送邮件失败!");
}
}