- 2024-02-05 10:13:36
- 8907 热度
- 0 评论
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
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Eclipse(16)
- Vue(16)
- Git(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- ueditor(6)
- jar(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- star(6)
- and(6)
- Struts(5)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- discuz(5)