版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34833599/article/details/82928364
1.邮件的用途
- 在现在的程序中web进行信息的验证基本都是邮箱,图片验证随着人工只能的发展似乎变得易于破解,而短信验证则需要一些费用,而对于个人的小网站还是用邮件进行发送验证码更好。
2.简单邮件的发送
- 只进行一些文字的发送可以用这个实现,但是太过于简单,而基本的都是进行激活和验证码强调所以这种基本解决不了验证的问题。
// 发送的代码:
public void sendSimpleMail(String to,String subject,String content){
/**
*
* 功能描述: 发送简单的邮件
* to : 目标邮箱
* subject:标题
* content:内容
* from:发送人邮箱
*
* @param: [to, subject, content]
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:48
*/
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject(subject);
simpleMailMessage.setTo(to);
simpleMailMessage.setText(content);
simpleMailMessage.setFrom(from);
mailSender.send(simpleMailMessage);
}
@Test
public void MailServiceTest() {
/**
*
* 功能描述: 测试简单邮件的发送
*
* @param: []
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:55
*/
mailService.sendSimpleMail("[email protected]", "代码发个邮件", "测试");
}
3.HTML邮件的发送
- 对于web中的验证码和点击链接进行激活用HTML邮件的发送基本能够解决。
// 发送的方法
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
/**
*
* 功能描述: 发送HTML邮件
* to : 目标邮箱
* subject:标题
* content:内容
* from:发送人邮箱
* @param: [to, subject, content]
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:49
*/
MimeMessage mimeMailMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setFrom(from);
// 设置内容是HTML
helper.setText(content,true);
mailSender.send(mimeMailMessage);
}
@Test
public void sendHtmlMailTest() {
/**
*
* 功能描述: 测试HTML邮件的发送
*
* @param: []
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:56
*/
String content = "<html><body><h3>第一个HTML邮件</h3></body></html>";
try {
mailService.sendHtmlMail("[email protected]", "HTML邮件测试", content);
} catch (MessagingException e) {
e.printStackTrace();
}
}
4.带附件的邮件的发送
- 带附件的链接很少发送,一般都是带有推广推销等的。
// 发送方法
public void sendAttachmentsMail(String to, String subject, String content,String filePath) throws MessagingException {
/**
*
* 功能描述: 发送带有附件的邮件
* to:收件人的邮箱
* subject:主题
* content:内容
* filePath:附件的文件地址 目前网络路径失败
* @param: [to, subject, content, filePath]
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:51
*/
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from);
helper.setTo(to);
helper.setText(content,true);
helper.setSubject(subject);
FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
String fileName = fileSystemResource.getFilename();
helper.addAttachment(fileName,fileSystemResource);
mailSender.send(mimeMessage);
}
@Test
public void sendAttachmentsMailTest() {
/**
*
* 功能描述: 测试带有附件多的邮件的发送
*
* @param: []
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:56
*/
try {
mailService.sendAttachmentsMail("[email protected]", "带有附件的邮件", "测试带有附件的邮件", "C:\\Users\\96109\\Pictures" +
"\\timg.jpg");
} catch (MessagingException e) {
e.printStackTrace();
}
}
5.图片邮件的发送
- 图片邮件的发送一般是为了美观,例如朋友间在QQ邮箱发邮件就可以选择背景图片,这就相当于那样。
public void sendInlinResourceMail(String to, String subject, String content,String rscPath,String rsId) throws MessagingException {
/**
*
* 功能描述: 发送图片邮件
* to:收件人的邮箱
* subject:主题
* content:内容(包含图片的内容即:HTML格式,可以用thymeleaf模版)
* rscPath: 图片的路径
* rsId:图片的编号,用于生成个content
*
* @param: [to, subject, content, rscPath, rsId]
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:52
*/
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setFrom(from);
helper.setText(content,true);
helper.setSubject(subject);
FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
helper.addInline(rsId,fileSystemResource);
mailSender.send(mimeMessage);
}
@Test
public void sendInlinResourceMailTest() throws MessagingException {
/**
*
* 功能描述: 测试图片邮件的发送
*
* @param: []
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:56
*/
String rscPath = "C:\\Users\\96109\\Pictures" +"\\timg.jpg";
String rsId = "img001";
String content = "<html><body>这是一个带图片的邮件:<img src=\'cid:"+rsId+"\'></img></body></html>";
mailService.sendInlinResourceMail("[email protected]","图片邮件",content,rscPath,rsId);
}
6.模版邮件的发送
- 模版邮件的发送其实和HTML邮件的发送一直,只不过发送的邮件不再是自己手动拼成的字符串了,而是利用thymeleaf的模版页面,利用TemplateEngine进行获取页面信息并且动态插入数据。这里引用了thymeleaf,所以需要引入相关的包,创建TemplateEngine属性。
@Test
public void sendTemplatesMail() throws MessagingException {
/**
*
* 功能描述: 测试根据thymeleaf模版发送HTML邮件
*
* @param: []
* @return: void
* @auther: ZhangJiahao
* @date: 2018/10/2 19:56
*/
Context context = new Context();
context.setVariable("id","100");
String emailContent = templateEngine.process("templatesMail",context);
mailService.sendHtmlMail("[email protected]", "这是一个模版邮件", emailContent);
}
thymeleaf模版文件,文件名为:templatesMail.html
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html;charset=UTF-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>激活</title>
</head>
<body>
欢迎注册<br>
<a href="#" th:href="@{http://www.baidu.com/{id}(id=${id})}">激活帐号</a>
</body>
</html>
7.相关配置
- application.properties中的配置内容:
# 根据不同的邮箱选择不同的smtp QQ: smtp.qq.com
spring.mail.host=smtp.163.com
# 邮箱号
[email protected]
# 可以在相关多的邮箱设置页面获取相关的安全码 并不是登录密码
spring.mail.password=**
# 设置编码方式utf-8
spring.mail.default-encoding=utf-8