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

写在前面

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

javamail 使用网易163邮箱发送邮件报错:554 DT:SPM_第1张图片

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

解决方法

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

方法实现

// 设置属性
private Properties p; // p是属性集合类,用来设置邮件的一些属性比如timeout等
p.put("userName","[email protected]"); //这里填上你的邮箱(发送方)
// 构件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 arrayList){
        mailSender = new JavaMailSenderImpl();
        p = new Properties();
        content = new StringBuilder();
        initMail(mailSender,p,arrayList);
    }

    private void initMail(JavaMailSenderImpl mailSender, Properties p,ArrayList arrayList){
        // 设置参数
        mailSender.setHost("smtp.163.com");
        mailSender.setUsername("[email protected]"); //你的邮箱
        mailSender.setPassword("xxx"); //密码,这里必须先在网易邮箱里开启smtp服务
        //设置property
        p.put("userName","[email protected]");
        p.setProperty("mail.smtp.timeout",timeout+"");
        p.setProperty("mail.smtp.auth","true");
        mailSender.setJavaMailProperties(p);
        //设置内容格式
        content.append("");
        content.append("Hi. This is the New Grade·Inform.");
        content.append("
"
); //简单版本 /*String text; for (Grade grade:arrayList) { text = ""; text += grade.toString(); content.append(text); content.append("
"); }*/
//表格版本 content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); for (Grade grade: arrayList) { content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); content.append(""); } content.append(""); content.append("
课程名分数绩点学分属性
"+grade.getCourseName()+""+grade.getScore()+""+grade.getGpa()+""+grade.getCredit()+""+grade.getAttribute()+"
"
); content.append("
"
); content.append("Thank you! "); } /** * * @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..

你可能感兴趣的:(问题)