spring boot 用javaMail发送邮件,很多坑

直接发送总是报错 554 dt:spm 被163拦截,认为非法,抄送一份给自己就解决了。但是显示抄送人,很烦。

service层

package com.llong.email.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class SendByEmailTools {

@Autowired
JavaMailSender jms;

public  String send(String sender,String receiver,String cc ,String bcc, String title,String text){




    //建立邮件消息
    SimpleMailMessage mainMessage = new SimpleMailMessage();
    //发送者

    mainMessage.setFrom(sender);

    //接收者
    mainMessage.setTo(receiver);

    //设置抄送者
    mainMessage.setCc(cc);

    //设置暗送者
    mainMessage.setBcc(bcc);

    //发送的标题
    mainMessage.setSubject(title);
    //发送的内容
    mainMessage.setText(text);
    jms.send(mainMessage);
    return "1";
}

}

controller层

package com.llong.email.mail;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**

  • @author mcb
  • 2018年5月4日 下午3:52:30

*/
@RestController
public class SendByEmailController {
@Autowired
private SendByEmailTools service;

@GetMapping("/send")
public String send(){



    String result=service.send("[email protected]","[email protected]","[email protected]","[email protected]","找回密码成功","你的密码是爱仕达答641132sss");

    return result;
}

}

application.yml
spring:
mail:
host: smtp.163.com
username:[email protected]
password: xxxx
protocol: smtp
properties.mail.smtp.auth: true
properties.mail.smtp.port: 465
properties.mail.display.sendmail: Javen
properties.mail.display.sendname: Spring Boot Guide Email
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
from: [email protected]

猜你喜欢

转载自blog.csdn.net/weixin_42844971/article/details/84998179