Spring Boot 发送邮件

pom.xml配置

添加如下依赖:

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-mailartifactId>
dependency>

邮件配置

在application.properties添加主机、用户名、密码、协议、端口号、编码等,例如:

spring.mail.host=smtp.partner.***
spring.mail.username=stationt@***
spring.mail.password=g7****
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.port=25
spring.mail.default-encoding=UTF-8

发送普通邮件

新家一个Controller类:MailController1.java ,代码如下:

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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by pc on 2018/7/3.
 */
@RestController
public class MailController1 {
    @Autowired
    JavaMailSender mailSender;   //自动注入邮件配置

    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数

    @RequestMapping(value="/sendSimpleMail")
    public String sendSimpleMail()
    {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Sender);   //邮件发送人
        message.setTo("[email protected]");  //邮件接收人
        message.setSubject("测试邮件");   //邮件主题
        message.setText("灵枢测试");   //邮件内容
        mailSender.send(message);
        return "邮件发送成功";
    }
}

运行Application.java,启动web服务:
Spring Boot 发送邮件_第1张图片

在浏览器输入:http://localhost:8102/sendSimpleMail 就执行发送邮件操作了,如下图。注意我这里重新设置了端口号,所以是8102。
Spring Boot 发送邮件_第2张图片

收到的邮件:
Spring Boot 发送邮件_第3张图片

发送HTML的邮件

再在MailController1.java里添加一个方法,如下:

@RequestMapping(value="/sendHtmlMail")
    public String sendHtmlMail()
    {
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom(Sender);
            helper.setTo("404****[email protected]");
            helper.setSubject("测试HTML邮件");
            StringBuffer stringBuffer = new StringBuffer();
            //添加HTML格式的邮件内容
            stringBuffer.append("业务可能出现异常!");
            helper.setText(stringBuffer.toString(), true);
        }catch (Exception e){
            e.printStackTrace();
        }
        mailSender.send(message);
        return "发送HTML邮件成功";
    }

运行Application.java,重新启动web服务器

在浏览器输入:http://localhost:8102/sendHtmlMail,回车,响应如下图:
Spring Boot 发送邮件_第4张图片

收到的邮件如下:
Spring Boot 发送邮件_第5张图片

群发邮件

在sendHtmlMail方法里,把helper.setTo()改为传一个String数组即可,如:

@RequestMapping(value="/sendHtmlMail")
    public String sendHtmlMail()
    {
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom(Sender);
//            helper.setTo("404****[email protected]");
            //群发邮件
            String[] mails={"404***[email protected]","814****[email protected]"};
            helper.setTo(mails);
            helper.setSubject("测试HTML邮件");
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("业务可能出现异常!");
            helper.setText(stringBuffer.toString(), true);
        }catch (Exception e){
            e.printStackTrace();
        }
        mailSender.send(message);
        return "发送HTML邮件成功";
    }

你可能感兴趣的:(java,Spring,Boot,Spring,Boot,java)