Java发送邮件,使用Spring发送邮件,之前都写过,一直很火的一个SpringBoot,什么是SpringBoot我就不多说了,因为我也不知道什么是SpringBoot,我是真不知道,因为我没用过。
最近学习,看别人有用SpringBoot发送邮件,跟着学了点,感觉不错,于是总结一下,我可不希望以后忘了再去慢悠悠的看一遍视频,仅此而已。
首先是去Spring官网https://start.spring.io下一个Demo,然后倒入到开发工具中,我一直使用MyEclipse。

如图选择Java版的Maven工程,版本为1.5.16,Group和Artifact自己看着写,依赖包选上Mail和Thymeleaf(什么是Thymeleaf自己查,因为我也没用过)。

倒入到开发工具后,新建一个类用来写发送邮件的代码,再写一个测试类。templates下的test.html是模版,用来测试模版邮件。
挨个测试,包括检查工程是否能正常启动,发送普通邮件,HTML邮件,附件邮件,内嵌图片邮件,模版邮件。其中内嵌图片的邮件不用太关注,因为用处不大。
其中带附件和内嵌图片的效果如下图


使用Junit进行测试即可,SpringBoot会启动Spring容器和加载配置文件。
代码下载:
核心代码如下:
package com.hd.mail.service;
import java.io.File;
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.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
/**
* 发送邮件
*/
@Service
public class MailService {
// 直接读取application.properties配置文件
@Value("${spring.mail.username}")
private String from;
@Autowired private JavaMailSender mailSender;
/**
* 发送内嵌图片的邮件
* @param to
* @param subject
* @param content
* @param picId
* @param picPath
* @throws MessagingException
*/
public void sendMailInLine(String to,String subject,String content,String picId,String picPath) throws MessagingException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
// 设置图片
FileSystemResource file = new FileSystemResource(new File(picPath));
helper.addInline(picId, file);
helper.setFrom(from);
mailSender.send(mimeMessage);
}
/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
* @throws MessagingException
*/
public void sendMailAtt(String to,String subject,String content,String filePath) throws MessagingException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
// 设置附件
FileSystemResource file = new FileSystemResource(new File(filePath));
String fname = file.getFilename();
helper.addAttachment(fname, file);//附件可以添加多个
helper.setFrom(from);
mailSender.send(mimeMessage);
}
/**
* 发送HTML邮件
* @param to
* @param subject
* @param content
* @throws MessagingException
*/
public void sendMailHtml(String to,String subject,String content) throws MessagingException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true); // 标记为HTML
helper.setFrom(from);
mailSender.send(mimeMessage);
}
/**
* 发送普通文本
* @param to
* @param subject
* @param content
*/
public void sendMailText(String to,String subject,String content){
SimpleMailMessage m = new SimpleMailMessage();
m.setTo(to);
m.setSubject(subject);
m.setText(content);
m.setFrom(from);
mailSender.send(m);
}
/**
* 简单调用测试
*/
public void sendMail(){
System.out.println("send mail success");
}
}测试代码如下:
package com.hd.mail.service;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {
@Resource MailService mailService;
@Autowired private TemplateEngine templateEngine;
private String to = "111111@qq.com";
@Test
public void sendMailTemplate(){
Context context = new Context();
context.setVariable("para1", "java小强");
String emailStr = templateEngine.process("test", context);
try {
mailService.sendMailHtml(to, "来自测试HTML", emailStr);
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Test
public void sendMailInLine(){
try {
String picId1 = "picId1";
String picPath1 = "D:\\temp\\test.jpg";
String content = "<html><body><p style=\"font-size:30px\">来自测试内嵌图片</p><img src=\'cid:"+picId1+"\'></img></html></body>";
mailService.sendMailInLine(to, "来自测试内嵌图片", content, picId1, picPath1);
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Test
public void sendMailAtt(){
try {
String content = "<p style=\"font-size:30px\">来自测试附件</p>";
String filePath = "D:\\temp\\test.txt";
mailService.sendMailAtt(to, "来自测试附件", content, filePath);
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Test
public void sendMailHtml(){
try {
mailService.sendMailHtml(to, "来自测试HTML", "<p style=\"font-size:30px\">来自测试HTML</p>");
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Test
public void sendMailText(){
mailService.sendMailText(to, "来自测试", "这是文本邮件测试");
}
@Test
public void sendMailTest(){
mailService.sendMail();
}
}其他可以参考:
Spring 发送邮件 简单邮件 http://www.javacui.com/framework/81.html
Spring 发送邮件 HTML邮件 http://www.javacui.com/opensource/82.html
Spring 发送邮件 内嵌图片增加附件 http://www.javacui.com/framework/83.html
Spring 发送邮件 使用File指定附件 http://www.javacui.com/framework/84.html
JavaMail邮件发送-发送非纯文本邮件 http://www.javacui.com/java/394.html
JavaMail邮件发送-发送一个文本邮件和一些问题说明 http://www.javacui.com/java/395.html
JavaMail邮件发送-将邮件保存到本地和发送一封本地邮件 http://www.javacui.com/java/396.html
JavaMail邮件发送-发送带附件的邮件 http://www.javacui.com/java/397.html
JavaMail邮件发送-为你的邮件增加背景音乐和背景图片 http://www.javacui.com/java/398.html
JavaMail邮件发送-能发送附件和带背景音乐的邮件的小系统 http://www.javacui.com/java/399.html
Java小强
未曾清贫难成人,不经打击老天真。
自古英雄出炼狱,从来富贵入凡尘。
发表评论: