javamail 使用网易163邮箱发送邮件报错:554 DT:SPM

时间:2021-03-27 18:59:24

写在前面

想必大家在用javamail使用网易邮箱发送邮件时会遇到这样一个蛋疼的错误:554 DT:SPM

javamail 使用网易163邮箱发送邮件报错:554 DT:SPM

网上找了、也试了很多种方法。这里贴一个亲测有效的方法。

解决方法

方法: 在发送给收信人之前给自己抄送一份

方法实现

// 设置属性
private Properties p; // p是属性集合类,用来设置邮件的一些属性比如timeout等
p.put("userName","xxx@163.com"); //这里填上你的邮箱(发送方)
// 构件MimeMessage 对象,并设置在发送给收信人之前给自己(发送方)抄送一份
MimeMessage msg = mailSender.createMimeMessage();
msg.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(p.getProperty("userName")));

完整代码

package com.hzy.gradeinform.Util;

import com.hzy.gradeinform.Model.Grade;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.Properties;

/**
* Created by huangzhenyang on 2017/8/15.
*/

public class MailUtil {
// Spring的邮件工具类,实现了MailSender和JavaMailSender接口
private JavaMailSenderImpl mailSender;
private Properties p;
private int timeout = 25000;
private String subject = "Grade·Inform";
private StringBuilder content;

public MailUtil(ArrayList<Grade> arrayList){
mailSender = new JavaMailSenderImpl();
p = new Properties();
content = new StringBuilder();
initMail(mailSender,p,arrayList);
}

private void initMail(JavaMailSenderImpl mailSender, Properties p,ArrayList<Grade> arrayList){
// 设置参数
mailSender.setHost("smtp.163.com");
mailSender.setUsername("xxx@163.com"); //你的邮箱
mailSender.setPassword("xxx"); //密码,这里必须先在网易邮箱里开启smtp服务
//设置property
p.put("userName","jobbridge@163.com");
p.setProperty("mail.smtp.timeout",timeout+"");
p.setProperty("mail.smtp.auth","true");
mailSender.setJavaMailProperties(p);
//设置内容格式
content.append("<body>");
content.append("<span style=\"font-size:15px\">Hi. This is the New Grade·Inform.</span>");
content.append("<hr>");
//简单版本
/*String text;
for (Grade grade:arrayList) {
text = "";
text += grade.toString();
content.append(text);
content.append("<br>");
}*/

//表格版本
content.append("<table>");
content.append("<thead>");
content.append("<tr>");
content.append("<th>课程名</th>");
content.append("<th>分数</th>");
content.append("<th>绩点</th>");
content.append("<th>学分</th>");
content.append("<th>属性</th>");
content.append("</tr>");
content.append("</thead>");
content.append("<tbody>");
for (Grade grade: arrayList) {
content.append("<tr>");
content.append("<td style=\"text-align:center\">"+grade.getCourseName()+"</td>");
content.append("<td>"+grade.getScore()+"</td>");
content.append("<td>"+grade.getGpa()+"</td>");
content.append("<td>"+grade.getCredit()+"</td>");
content.append("<td>"+grade.getAttribute()+"</td>");
content.append("</tr>");
}
content.append("</tbody>");
content.append("</table>");

content.append("<hr>");
content.append("<span style=\"font-size:17px\">Thank you! </span></body>");
}

/**
*
* @param recipient 收件人
*
*
*/


public void send(String recipient) throws MessagingException {
// 构建简单邮件对象,见名知意
MimeMessage msg = mailSender.createMimeMessage();
// *** 关键 ***
msg.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(p.getProperty("userName")));
MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8");
// 设定邮件参数
helper.setFrom(mailSender.getUsername());
helper.setTo(recipient);
helper.setSubject(subject);
helper.setText(content.toString(),true);
System.out.println(">>> content: "+content);
// 发送邮件
mailSender.send(msg);
}

}

写在后面

网易邮箱的554 DT:SPM真的是巨坑天坑神坑!!! 这个方法虽然很蠢……不过好歹是解决了。Hmmm..