场景
甲客户访问B的公司官网时,发现乙公司的产品可以引进,所以想通过官网客户留言的方式与乙沟通。
客户在某公司官网留言是不需要登录邮箱的,甲客户填入自己的留言,点击提交。这时候就需要乙公司的A邮箱发送邮件到乙公司的B邮箱,邮件内容为甲客户的留言信息。
发送人和接收人均为乙公司,只不过使用不同的邮箱
springboot集成的mail
这种方式发送邮件,需要几个必需的参数:发送人邮箱服务器(host)、发送人账号(username)、发送人授权码(password)。
邮箱服务器可以去搜索一下,qq邮箱:smtp.qq.com 163邮箱:smtp.163.com
授权码,需要登录发送人邮箱,从账号设置里面进行获取,开通POP3/SMTP提供的授权码,如果没有授权码,使用真实密码
我用的是163的邮箱,所以以163的为例子
引入maven依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
配置application.yml
1 2 3 4 5 6 7
| spring: mail: host: smtp.163.com username: zhansan@163.com password: KIRLYASCYAZIOZHD protocol: smtp default-encoding: UTF-8
|
编写邮件工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import com.dakj.entity.Email; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.web.bind.annotation.*; public class EmailUtils { @Autowired private JavaMailSender mailSender; public String sendEmail(@RequestBody Email email) { String customerName = "李先生"; String customerTelphone = "15088888888"; String customerContent = "看了下你们的产品,我很满意,可以一起合作吗?"; String content = "\n客户姓名:" + customerName + "\n客户手机:" + customerTelphone + "\n客户问题:" + customerContent; SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom("zhangsan111@163.com"); simpleMailMessage.setTo("zhangsan222@163.com"); simpleMailMessage.setSubject("==== 客户留言 ===="); simpleMailMessage.setText(content); mailSender.send(simpleMailMessage); return "发送邮件成功!"; } }
|
springboot集成的mail(自定义邮件模板模式)
引入maven依赖
1 2 3 4 5 6 7 8 9 10 11
| <!-- mail --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
<!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
|
创建application.yml文件
1 2 3 4 5 6 7 8
| spring: mail: host: smtp.qq.com port: 587 username: test@qq.com password: asdadazxqsdas protocol: smtp default-encoding: UTF-8
|
创建springboot-email启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context;
import cn.tyrone.springboot.mail.util.MailBean; import cn.tyrone.springboot.mail.util.MailUtil;
@SpringBootApplication public class Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
@Autowired MailUtil mailUtil; @Autowired TemplateEngine templateEngine;
public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }
@Override public void run(String... args) throws Exception { log.info("------------- SpringBoot集成JavaMail实现邮件发送开始 -------------"); MailBean mailBean = MailBean.getMailBean();
Context context = new Context(); context.setVariable("user", "Tyrone"); String content = templateEngine.process("emailTemplate", context); mailBean.setSubject("SpringBoot集成JavaMail实现邮件发送"); mailBean.setText(content); mailUtil.sendMailTemplate(mailBean);
log.info("------------- SpringBoot集成JavaMail实现邮件发送结束 -------------"); } }
|
为了便于测试,启动类直接实现CommandLineRunner接口,并重写run方法来实现邮件发送的测试
创建发送邮件工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| import javax.mail.MessagingException; import javax.mail.internet.MimeMessage;
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.Component;
@Component public class MailUtil {
@Autowired private JavaMailSender mailSender;
@Value("${spring.mail.username}") private String sender;
public void sendMail() { SimpleMailMessage mimeMessage = new SimpleMailMessage(); mimeMessage.setFrom(sender); mimeMessage.setTo(sender); mimeMessage.setSubject("SpringBoot集成JavaMail实现邮件发送"); mimeMessage.setText("SpringBoot集成JavaMail实现邮件发送正文"); mailSender.send(mimeMessage); }
public void sendMail(MailBean mailBean) { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
try { helper.setFrom(sender); helper.setTo(sender); helper.setSubject(mailBean.getSubject()); helper.setText(mailBean.getText()); } catch (MessagingException e) { e.printStackTrace(); }
mailSender.send(mimeMessage); }
public void sendMailHtml(MailBean mailBean) { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
try { helper.setFrom(sender); helper.setTo(sender); helper.setSubject(mailBean.getSubject()); helper.setText(mailBean.getText(), true); } catch (MessagingException e) { e.printStackTrace(); }
mailSender.send(mimeMessage); }
public void sendMailAttachment(MailBean mailBean) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(sender); helper.setTo(sender); helper.setSubject(mailBean.getSubject()); helper.setText(mailBean.getText(), true); helper.addAttachment(mailBean.getAttachmentFilename(), mailBean.getFile()); mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } }
public void sendMailInline(MailBean mailBean) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(sender); helper.setTo(sender); helper.setSubject(mailBean.getSubject());
helper.setText(mailBean.getText(), true); helper.addInline(mailBean.getContentId(), mailBean.getFile());
mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } }
public void sendMailTemplate(MailBean mailBean) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(sender); helper.setTo(sender); helper.setSubject(mailBean.getSubject()); helper.setText(mailBean.getText(), true); mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } }
}
|
创建Mail实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import org.springframework.core.io.FileSystemResource; import lombok.Data;
@Data public class MailBean {
private String subject;
private String text;
private FileSystemResource file;
private String attachmentFilename;
private String contentId;
public static MailBean getMailBean() { return new MailBean(); }
}
|
创建邮件模板文件
1 2 3 4 5 6 7 8 9 10 11
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8" /> <title>Insert title here</title> </head> <body> <p>Hello!<span th:text="${user}"></span>!<br /></p> 这是SpringBoot集成Java Email,并通过Thymeleaf创建邮件模板,实现的模板邮件发送!!! </body> </html>
|
注意:根据约定大于配置,模板文件建议放在resources
目录下。本例:/resources/templates/emailTemplate.html