springboot 简单邮件发送

写作原因:

项目接近尾声,需求一变再变,其实技术点从未改变,只是业务逻辑的变更,发送邮件提醒的功能,两个月变更七次。我想把技术点记录下来,这里无关乎业务,只有发送邮件的功能。

邮件发送准备说明:

由于公司项目需求,所以我们使用的邮箱是本公司内部邮箱,所以部门给我们系统提供的邮箱是国际邮箱,也就是最高权限邮箱。所以邮箱配置,需要根据提供邮箱的形式配置。

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.yml

spring:
  mail:
    host: smtp.xxx.com
    port: 25
    username: [email protected]
    password: xxx
    default-encoding: UTF-8
  thymeleaf: false

注意:host:属性默认是 JavaMail 会话的主机;
port:端口默认监听标准的 SMTP 端口25,如果公司内部禁用的话,可以采用465;
username:发送方的邮箱;
password:邮箱密码,如果是qq或者163邮箱的话,是邮箱授权码;
default-encoding:邮件编码字符集;
thymeleaf:模板生成email,Spring 给出的解决方案是 使用模板生成Email,有多种模板方案可供选择;

controller :

import io.swagger.annotations.Api;
​
​
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
@RequestMapping("mail")
@Api(description = "邮件发送")
public class MailSendController {
​
    @Autowired
    private IMailService service;
​
    @PostMapping("/sendText")
    public ResponseStatus send() {
        return service.sendMail();
    }
}

service:

import xxx.xxx.xxx.xxx.ResponseStatus;
​
​
public interface IMailService {
​
    public void sendMail();
}

IMailServiceImpl:

import xxx.xxx.xxx.xxx.IMailService;

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

import javax.annotation.Resource;
import java.util.Date;


@Service
public class IMailServiceImpl implements IMailService {

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;


    @Override
    public void sendMail(String title, String text, String to) {
        //邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        
        //邮件主题
        String title = "我是测试邮件的主题";
        //邮件内容
        String text = "我是这封测试邮件的内容,内容多多,可以根据自身业务填充";
        //接收邮件人邮箱
        String to = "xxx@xxx";
        
        
        // 发送人的邮箱(系统配置的邮箱)
        message.setFrom(from); 
        //发给谁对方邮箱(接收有邮件人邮箱)
        message.setTo(to);
        //标题
        message.setSubject(title); 
        //内容
        message.setText(text); 
        //发件日期
        message.setSentDate(new Date());
        //发送
        javaMailSender.send(message); 
        ResponseStatus.ok(); 
    }
}

总结:
1、yum文件中邮箱配置需要特别注意,不同邮箱在配置password的时候,应特别注意,企业自己邮箱未经设置的话,是邮箱密码,我采用的这种形式。QQ邮箱及163邮箱在此处是邮箱授权码,如何取得授权码可自行问“度娘”。
2、邮件端口设置默认25,如果端口被占用的话使用465。
3、SpringBoot中集成邮件服务记录,小白成长中,望不吝赐教。

本文作者:魂皓轩 欢迎关注公众号

本人保留所有权益,转载请注明出处。
欢迎有故事、有想法的朋友和我分享,可发送至 e-mail: [email protected]

猜你喜欢

转载自www.cnblogs.com/lwqforit/p/11982968.html