diff --git a/README.md b/README.md index 7c2a4b3..3b1d5fa 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开 - [spring-boot-mybatis](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis):Spring Boot 3.0 Mybatis 注解、xml 使用、多数据源使用示例 - [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-rabbitmq):Spring Boot 3.0 RabbitMQ 各种常见场景使用示例 - [spring-boot-scheduler](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-scheduler):Spring Boot 3.0 定时任务 scheduler 使用示例 ---- +- [spring-boot-mail](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mail):Spring Boot 3.0 邮件发送使用示例 > 如果大家想了解关于 Spring Boot 的其它方面应用,也可以以[issues](https://github.com/ityouknow/spring-boot-examples/issues)的形式反馈给我,我后续来完善。 diff --git a/spring-boot-mail/pom.xml b/spring-boot-mail/pom.xml new file mode 100644 index 0000000..f98e3ec --- /dev/null +++ b/spring-boot-mail/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + com.neo + spring-boot-mail + 1.0.0 + jar + + spring-boot-mail + Demo project for Spring Boot and mail + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + + UTF-8 + 17 + + + + + org.springframework.boot + spring-boot-starter-mail + + + org.springframework + spring-context-support + RELEASE + + + com.sun.mail + javax.mail + RELEASE + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + + + org.hamcrest + hamcrest-core + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + diff --git a/spring-boot-mail/src/main/java/com/neo/MailApplication.java b/spring-boot-mail/src/main/java/com/neo/MailApplication.java new file mode 100644 index 0000000..c0aee41 --- /dev/null +++ b/spring-boot-mail/src/main/java/com/neo/MailApplication.java @@ -0,0 +1,12 @@ +package com.neo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MailApplication { + + public static void main(String[] args) { + SpringApplication.run(MailApplication.class, args); + } +} diff --git a/spring-boot-mail/src/main/java/com/neo/service/MailService.java b/spring-boot-mail/src/main/java/com/neo/service/MailService.java new file mode 100644 index 0000000..41df356 --- /dev/null +++ b/spring-boot-mail/src/main/java/com/neo/service/MailService.java @@ -0,0 +1,16 @@ +package com.neo.service; + +/** + * Created by summer on 2017/5/4. + */ +public interface MailService { + + void sendSimpleMail(String to, String subject, String content); + + void sendHtmlMail(String to, String subject, String content); + + void sendAttachmentsMail(String to, String subject, String content, String filePath); + + void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId); + +} diff --git a/spring-boot-mail/src/main/java/com/neo/service/impl/MailServiceImpl.java b/spring-boot-mail/src/main/java/com/neo/service/impl/MailServiceImpl.java new file mode 100644 index 0000000..5e9c5d5 --- /dev/null +++ b/spring-boot-mail/src/main/java/com/neo/service/impl/MailServiceImpl.java @@ -0,0 +1,140 @@ +package com.neo.service.impl; + +import com.neo.service.MailService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.Component; + +import jakarta.mail.MessagingException; +import jakarta.mail.internet.MimeMessage; +import java.io.File; + +/** + * Created by summer on 2017/5/4. + */ +@Component +public class MailServiceImpl implements MailService{ + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private JavaMailSender mailSender; + + @Value("${mail.fromMail.addr}") + private String from; + + /** + * 发送文本邮件 + * @param to + * @param subject + * @param content + */ + @Override + public void sendSimpleMail(String to, String subject, String content) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(from); + message.setTo(to); + message.setSubject(subject); + message.setText(content); + + try { + mailSender.send(message); + logger.info("简单邮件已经发送。"); + } catch (Exception e) { + logger.error("发送简单邮件时发生异常!", e); + } + + } + + /** + * 发送html邮件 + * @param to + * @param subject + * @param content + */ + @Override + public void sendHtmlMail(String to, String subject, String content) { + MimeMessage message = mailSender.createMimeMessage(); + + try { + //true表示需要创建一个multipart message + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + + mailSender.send(message); + logger.info("html邮件发送成功"); + } catch (MessagingException e) { + logger.error("发送html邮件时发生异常!", e); + } + } + + + /** + * 发送带附件的邮件 + * @param to + * @param subject + * @param content + * @param filePath + */ + @Override + public void sendAttachmentsMail(String to, String subject, String content, String filePath){ + MimeMessage message = mailSender.createMimeMessage(); + + try { + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + + FileSystemResource file = new FileSystemResource(new File(filePath)); + String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); + helper.addAttachment(fileName, file); + //helper.addAttachment("test"+fileName, file); + + mailSender.send(message); + logger.info("带附件的邮件已经发送。"); + } catch (MessagingException e) { + logger.error("发送带附件的邮件时发生异常!", e); + } + } + + + /** + * 发送正文中有静态资源(图片)的邮件 + * @param to + * @param subject + * @param content + * @param rscPath + * @param rscId + */ + @Override + public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ + MimeMessage message = mailSender.createMimeMessage(); + + try { + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + + FileSystemResource res = new FileSystemResource(new File(rscPath)); + helper.addInline(rscId, res); + + mailSender.send(message); + logger.info("嵌入静态资源的邮件已经发送。"); + } catch (MessagingException e) { + logger.error("发送嵌入静态资源的邮件时发生异常!", e); + } + } +} diff --git a/spring-boot-mail/src/main/resources/application.properties b/spring-boot-mail/src/main/resources/application.properties new file mode 100644 index 0000000..f7610b3 --- /dev/null +++ b/spring-boot-mail/src/main/resources/application.properties @@ -0,0 +1,10 @@ +spring.application.name=spirng-boot-mail + +spring.mail.host=smtp.126.com +spring.mail.username=xxxx@126.com +spring.mail.password=Zxxxxxxx + +spring.mail.default-encoding=UTF-8 + +mail.fromMail.addr=ixxxxw@126.com + diff --git a/spring-boot-mail/src/main/resources/templates/emailTemplate.html b/spring-boot-mail/src/main/resources/templates/emailTemplate.html new file mode 100644 index 0000000..387a4fb --- /dev/null +++ b/spring-boot-mail/src/main/resources/templates/emailTemplate.html @@ -0,0 +1,11 @@ + + + + + Title + + + 您好,这是验证邮件,请点击下面的链接完成验证,
+ 激活账号 + + \ No newline at end of file diff --git a/spring-boot-mail/src/test/java/com/neo/MailApplicationTests.java b/spring-boot-mail/src/test/java/com/neo/MailApplicationTests.java new file mode 100644 index 0000000..7a9e7a0 --- /dev/null +++ b/spring-boot-mail/src/test/java/com/neo/MailApplicationTests.java @@ -0,0 +1,17 @@ +package com.neo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class MailApplicationTests { + + @Test + public void contextLoads() { + System.out.println("hello world"); + } + +} diff --git a/spring-boot-mail/src/test/java/com/neo/service/MailServiceTest.java b/spring-boot-mail/src/test/java/com/neo/service/MailServiceTest.java new file mode 100644 index 0000000..4f49737 --- /dev/null +++ b/spring-boot-mail/src/test/java/com/neo/service/MailServiceTest.java @@ -0,0 +1,65 @@ +package com.neo.service; + +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; + +/** + * Created by summer on 2017/5/4. + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class MailServiceTest { + + @Autowired + private MailService mailService; + + @Autowired + private TemplateEngine templateEngine; + + @Test + public void testSimpleMail() throws Exception { + mailService.sendSimpleMail("ityouknow@126.com","test simple mail"," hello this is simple mail"); + } + + @Test + public void testHtmlMail() throws Exception { + String content="\n" + + "\n" + + "

hello world ! 这是一封html邮件!

\n" + + "\n" + + ""; + mailService.sendHtmlMail("ityouknow@126.com","test simple mail",content); + } + + @Test + public void sendAttachmentsMail() { + String filePath="D:\\Log\\TaxCard.log"; + mailService.sendAttachmentsMail("ityouknow@126.com", "主题:带附件的邮件", "有附件,请查收!", filePath); + } + + + @Test + public void sendInlineResourceMail() { + String rscId = "neo006"; + String content="这是有图片的邮件:"; + String imgPath = "C:\\Users\\ityou\\Pictures\\logo\\smilef.png"; + + mailService.sendInlineResourceMail("ityouknow@126.com", "主题:这是有图片的邮件", content, imgPath, rscId); + } + + + @Test + public void sendTemplateMail() { + //创建邮件正文 + Context context = new Context(); + context.setVariable("id", "006"); + String emailContent = templateEngine.process("emailTemplate", context); + + mailService.sendHtmlMail("ityouknow@126.com","主题:这是模板邮件",emailContent); + } +}