崛起于Springboot2.X+Mail邮件场景(57)

编程

《SpringBoot2.X心法总纲》

场景:用户角度就是授权登陆注册等...开发角度就是线上的项目发现bug异常,立即给开发发送bug到邮箱,快速发现问题解决问题。

1、pom依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-mail</artifactId>

</dependency>

2、application.properties

#mail

spring.mail.host=smtp.163.com

spring.mail.port=25

spring.mail.username=cj_javagopython@163.com

spring.mail.password=love15176305031

3、mail配置

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.JavaMailSenderImpl;

/**

* @Author:MuJiuTian

* @Description: 邮件配置

* @Date: Created in 下午3:04 2019/11/18

*/

@Configuration

@ConfigurationProperties(prefix = "spring.mail")

public class EmailConfig {

private String host;

private Integer port;

private String username;

private String password;

@Bean

public JavaMailSender javaMailSender() {

JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

javaMailSender.setHost(host);

javaMailSender.setPort(port);

javaMailSender.setUsername(username);

javaMailSender.setPassword(password);

return javaMailSender;

}

public void setHost(String host) {

this.host = host;

}

public void setPort(Integer port) {

this.port = port;

}

public void setUsername(String username) {

this.username = username;

}

public void setPassword(String password) {

this.password = password;

}

}

3、Service实现

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.mail.javamail.MimeMessageHelper;

import org.springframework.stereotype.Service;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import java.io.File;

@Service

public class MailService {

@Value("${spring.mail.username}")

private String setFrom;

@Autowired

JavaMailSender javaMailSender;

/**

* 纯文本的 Email 在于构造 SimpleMailMessage 实例

* @param setTo 收件人

* @param mailTitle 邮件标题

* @param mailContent 邮件内容

*/

public void sendSimpleEmail(String setTo, String mailTitle, String mailContent) {

// 构造Email消息

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom(setFrom);

message.setTo(setTo);

message.setSubject(mailTitle);

message.setText(mailContent);

javaMailSender.send(message);

}

/**

* 附件邮件发送

* @param setTo 手机人地址

* @param mailTitle 附件邮件主题

* @param mailContent 附件邮件内容

* @throws MessagingException

*/

public void mimeEmail(String setTo, String mailTitle, String mailContent, File file) throws MessagingException {

// MimeMessage 本身的 API 有些笨重,我们可以使用 MimeMessageHelper

MimeMessage mimeMessage = javaMailSender.createMimeMessage();

// 第二个参数是 true ,表明这个消息是 multipart类型的/

MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

mimeMessageHelper.setFrom(setFrom);

mimeMessageHelper.setTo(setTo);

mimeMessageHelper.setSubject(mailTitle);

mimeMessageHelper.setText(mailContent);

//添加附件,第一个参数表示添加到 Email 中附件的名称,第二个参数是图片资源

mimeMessageHelper.addAttachment("mail.png", file);

javaMailSender.send(mimeMessage);

}

/**

* 富文本发送,这个方法里面的参数根据自己的实际情况改变

*/

public void htmlEmail(String setTo, String mailTitle, String mailContent, File file) throws MessagingException {

// MimeMessage 本身的 API 有些笨重,我们可以使用 MimeMessageHelper

MimeMessage mimeMessage = javaMailSender.createMimeMessage();

// 第二个参数是 true ,表明这个消息是 multipart类型的,第三个防止中文乱码

MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true,"UTF-8");

mimeMessageHelper.setFrom(setFrom);

mimeMessageHelper.setTo(setTo);

mimeMessageHelper.setSubject(mailTitle);

StringBuilder html = new StringBuilder();

html.append("<h1>SpirngBoot测试邮件HTML</h1>")

.append(""<p style="color:#F00">"+mailContent+"</p>")

.append("<p style="text-align:right">右对齐</p>");

mimeMessageHelper.setText(html.toString(), true);

//添加附件,第一个参数表示添加到 Email 中附件的名称,第二个参数是图片资源

mimeMessageHelper.addAttachment("boot.png", file);

javaMailSender.send(mimeMessage);

}

}

4、Controller测试

@Autowired

MailService mailService;

/**

* mail 邮件发送测试

* @param type 1:简单发送 2:附件发送 3:富文本发送

* @throws MessagingException

*/

@GetMapping(value = "/mail")

public String mail(int type) throws MessagingException {

String setTo = "mdxlcj@qq.com";

String mailTitle = "邮箱个人简介";

String mailContent = "hhh,我是邮件的主要内容";

File file = new File("/Users/tentsuuhou/git/tssd/src/main/resources/static/images/tssd.png");

if (type == 1){

mailService.sendSimpleEmail(setTo, mailTitle, mailContent);

} else if (type == 2){

mailService.mimeEmail(setTo, mailTitle, mailContent,file);

} else {

mailService.htmlEmail(setTo,mailTitle,mailContent,file);

}

return "sci";

}

测试都成功!有疑问可以私信哈,我也不确定能不能让其他人看懂,但是代码是正确的。

 

 

以上是 崛起于Springboot2.X+Mail邮件场景(57) 的全部内容, 来源链接: utcz.com/z/510924.html

回到顶部