1. 程式人生 > >spring boot 用javaMail傳送郵件,很多坑

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]