From 5f14d18da7c58c12347985cbde4dc3f9d5bd4dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E7=A5=A5?= <1366971433@qq.com> Date: Fri, 28 Dec 2018 13:35:57 +0800 Subject: [PATCH] =?UTF-8?q?spring=20=E5=A2=9E=E5=8A=A0=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/spring-email-annotation/pom.xml | 91 +++++++++++ .../com/heibaiying/config/EmailConfig.java | 43 +++++ .../java/com/heibaiying/email/SpringMail.java | 147 ++++++++++++++++++ .../src/main/resources/beetl/template.html | 10 ++ .../test/java/com/heibaiying/SendEmail.java | 60 +++++++ spring/spring-email/pom.xml | 91 +++++++++++ .../java/com/heibaiying/email/SpringMail.java | 147 ++++++++++++++++++ .../src/main/resources/beetl/template.html | 10 ++ .../src/main/resources/springApplication.xml | 32 ++++ .../test/java/com/heibaiying/SendEmail.java | 59 +++++++ 10 files changed, 690 insertions(+) create mode 100644 spring/spring-email-annotation/pom.xml create mode 100644 spring/spring-email-annotation/src/main/java/com/heibaiying/config/EmailConfig.java create mode 100644 spring/spring-email-annotation/src/main/java/com/heibaiying/email/SpringMail.java create mode 100644 spring/spring-email-annotation/src/main/resources/beetl/template.html create mode 100644 spring/spring-email-annotation/src/test/java/com/heibaiying/SendEmail.java create mode 100644 spring/spring-email/pom.xml create mode 100644 spring/spring-email/src/main/java/com/heibaiying/email/SpringMail.java create mode 100644 spring/spring-email/src/main/resources/beetl/template.html create mode 100644 spring/spring-email/src/main/resources/springApplication.xml create mode 100644 spring/spring-email/src/test/java/com/heibaiying/SendEmail.java diff --git a/spring/spring-email-annotation/pom.xml b/spring/spring-email-annotation/pom.xml new file mode 100644 index 0000000..aeac2ce --- /dev/null +++ b/spring/spring-email-annotation/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + com.heibaiying + spring-email + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + 5.1.3.RELEASE + + + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + org.springframework + spring-context-support + ${spring-base-version} + + + javax.mail + mail + 1.4.7 + + + + org.springframework + spring-test + 5.1.3.RELEASE + test + + + junit + junit + 4.12 + test + + + + + com.ibeetl + beetl + 2.9.7 + + + \ No newline at end of file diff --git a/spring/spring-email-annotation/src/main/java/com/heibaiying/config/EmailConfig.java b/spring/spring-email-annotation/src/main/java/com/heibaiying/config/EmailConfig.java new file mode 100644 index 0000000..cdf5bec --- /dev/null +++ b/spring/spring-email-annotation/src/main/java/com/heibaiying/config/EmailConfig.java @@ -0,0 +1,43 @@ +package com.heibaiying.config; + +import org.beetl.core.GroupTemplate; +import org.beetl.core.resource.ClasspathResourceLoader; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import java.io.IOException; + +/** + * @author : heibaiying + * @description : + */ + +@Configuration +@ComponentScan(value = "com.heibaiying.email") +public class EmailConfig { + + /*** + * 在这里可以声明不同的邮件服务器主机,通常是SMTP主机,而具体的用户名和时授权码则建议在业务中从数据库查询 + */ + @Bean(name = "qqMailSender") + JavaMailSenderImpl javaMailSender() { + JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); + javaMailSender.setHost("smtp.qq.com"); + javaMailSender.setPassword("587"); + return javaMailSender; + } + + /*** + * 配置模板引擎 + */ + @Bean + GroupTemplate groupTemplate() throws IOException { + //指定加载模板资源的位置 指定在classpath:beetl下- + ClasspathResourceLoader loader = new ClasspathResourceLoader("beetl"); + //beetl配置 这里采用默认的配置- + org.beetl.core.Configuration configuration = org.beetl.core.Configuration.defaultConfiguration(); + return new GroupTemplate(loader, configuration); + } +} diff --git a/spring/spring-email-annotation/src/main/java/com/heibaiying/email/SpringMail.java b/spring/spring-email-annotation/src/main/java/com/heibaiying/email/SpringMail.java new file mode 100644 index 0000000..31229ca --- /dev/null +++ b/spring/spring-email-annotation/src/main/java/com/heibaiying/email/SpringMail.java @@ -0,0 +1,147 @@ +package com.heibaiying.email; + +import org.beetl.core.GroupTemplate; +import org.beetl.core.Template; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.MailException; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Component; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.io.File; +import java.util.Map; + +/** + * @author : heibaiying + * @description : + */ +@Component +public class SpringMail { + + @Autowired + private JavaMailSenderImpl qqMailSender; + @Autowired + private GroupTemplate groupTemplate; + + /** + * 发送简单邮件 + * 在qq邮件发送的测试中,测试结果表明不管是简单邮件还是复杂邮件都必须指定发送用户, + * 且发送用户已经授权不然都会抛出异常: SMTPSendFailedException 501 mail from address must be same as authorization user + * qq 的授权码 可以在 设置/账户/POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 中开启服务后获取 + */ + public void sendTextMessage(String from, String authWord, String to, String subject, String content) { + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + SimpleMailMessage msg = new SimpleMailMessage(); + msg.setFrom(from); + msg.setTo(to); + msg.setSubject(subject); + msg.setText(content); + try { + // 发送消息 + this.qqMailSender.send(msg); + System.out.println("发送邮件成功"); + } catch (MailException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + + /** + * 发送带附件的邮件 + */ + public void sendEmailWithAttachments(String from, String authWord, String to, + String subject, String content, Map files) { + try { + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + MimeMessage message = qqMailSender.createMimeMessage(); + // 需要指定第二个参数为true 代表创建支持可选文本,内联元素和附件的多部分消息 + MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content); + // 传入附件 + for (Map.Entry entry : files.entrySet()) { + helper.addAttachment(entry.getKey(), entry.getValue()); + } + // 发送消息 + this.qqMailSender.send(message); + System.out.println("发送邮件成功"); + } catch (MessagingException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + + + /** + * 发送带内嵌资源的邮件 + */ + public void sendEmailWithInline(String from, String authWord, String to, + String subject, String content, File file) { + try { + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + MimeMessage message = qqMailSender.createMimeMessage(); + // 需要指定第二个参数为true 代表创建支持可选文本,内联元素和附件的多部分消息 + MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + // 使用true标志来指示包含的文本是HTML 固定格式资源前缀 cid: + helper.setText("", true); + // 需要先指定文本 再指定资源文件 + FileSystemResource res = new FileSystemResource(file); + helper.addInline("image", res); + // 发送消息 + this.qqMailSender.send(message); + System.out.println("发送邮件成功"); + } catch (MessagingException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + + /** + * 使用模板邮件 + */ + public void sendEmailByTemplate(String from, String authWord, String to, + String subject, String content) { + try { + Template t = groupTemplate.getTemplate("template.html"); + t.binding("subject", subject); + t.binding("content", content); + String text = t.render(); + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + MimeMessage message = qqMailSender.createMimeMessage(); + // 指定 utf-8 防止乱码 + MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + // 为true 时候 表示文本内容以 html 渲染 + helper.setText(text, true); + this.qqMailSender.send(message); + System.out.println("发送邮件成功"); + } catch (MessagingException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + +} diff --git a/spring/spring-email-annotation/src/main/resources/beetl/template.html b/spring/spring-email-annotation/src/main/resources/beetl/template.html new file mode 100644 index 0000000..f8245f6 --- /dev/null +++ b/spring/spring-email-annotation/src/main/resources/beetl/template.html @@ -0,0 +1,10 @@ + + + + + + +

邮件主题: ${subject}

+

${content}

+ + \ No newline at end of file diff --git a/spring/spring-email-annotation/src/test/java/com/heibaiying/SendEmail.java b/spring/spring-email-annotation/src/test/java/com/heibaiying/SendEmail.java new file mode 100644 index 0000000..e2a3362 --- /dev/null +++ b/spring/spring-email-annotation/src/test/java/com/heibaiying/SendEmail.java @@ -0,0 +1,60 @@ +package com.heibaiying; + +import com.heibaiying.config.EmailConfig; +import com.heibaiying.email.SpringMail; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * @author : heibaiying + * @description : 发送邮件测试类 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = EmailConfig.class) +public class SendEmail { + + @Autowired + private SpringMail springMail; + + // 发送方邮箱地址 + private static final String from = "发送方邮箱地址@qq.com"; + // 发送方邮箱地址对应的授权码 + private static final String authWord = "授权码"; + // 接收方邮箱地址 + private static final String to = "接收方邮箱地址@qq.com"; + + @Test + public void sendMessage() { + + springMail.sendTextMessage(from, authWord, to, "spring简单邮件", "Hello Spring Email!"); + } + + + @Test + public void sendComplexMessage() { + Map fileMap = new HashMap<>(); + fileMap.put("image1.jpg", new File("D:\\LearningNotes\\picture\\msm相关依赖.png")); + fileMap.put("image2.jpg", new File("D:\\LearningNotes\\picture\\RabbitMQ模型架构.png")); + springMail.sendEmailWithAttachments(from, authWord, to, "spring多附件邮件" + , "Hello Spring Email!", fileMap); + } + + @Test + public void sendEmailWithInline() { + springMail.sendEmailWithInline(from, authWord, to, "spring内嵌资源邮件" + , "Hello Spring Email!", new File("D:\\LearningNotes\\picture\\RabbitMQ模型架构.png")); + } + + @Test + public void sendEmailByTemplate() { + springMail.sendEmailByTemplate(from, authWord, to, + "spring模板邮件", "Hello Spring Email!"); + } +} diff --git a/spring/spring-email/pom.xml b/spring/spring-email/pom.xml new file mode 100644 index 0000000..aeac2ce --- /dev/null +++ b/spring/spring-email/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + com.heibaiying + spring-email + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + 5.1.3.RELEASE + + + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + org.springframework + spring-context-support + ${spring-base-version} + + + javax.mail + mail + 1.4.7 + + + + org.springframework + spring-test + 5.1.3.RELEASE + test + + + junit + junit + 4.12 + test + + + + + com.ibeetl + beetl + 2.9.7 + + + \ No newline at end of file diff --git a/spring/spring-email/src/main/java/com/heibaiying/email/SpringMail.java b/spring/spring-email/src/main/java/com/heibaiying/email/SpringMail.java new file mode 100644 index 0000000..31229ca --- /dev/null +++ b/spring/spring-email/src/main/java/com/heibaiying/email/SpringMail.java @@ -0,0 +1,147 @@ +package com.heibaiying.email; + +import org.beetl.core.GroupTemplate; +import org.beetl.core.Template; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.MailException; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Component; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.io.File; +import java.util.Map; + +/** + * @author : heibaiying + * @description : + */ +@Component +public class SpringMail { + + @Autowired + private JavaMailSenderImpl qqMailSender; + @Autowired + private GroupTemplate groupTemplate; + + /** + * 发送简单邮件 + * 在qq邮件发送的测试中,测试结果表明不管是简单邮件还是复杂邮件都必须指定发送用户, + * 且发送用户已经授权不然都会抛出异常: SMTPSendFailedException 501 mail from address must be same as authorization user + * qq 的授权码 可以在 设置/账户/POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 中开启服务后获取 + */ + public void sendTextMessage(String from, String authWord, String to, String subject, String content) { + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + SimpleMailMessage msg = new SimpleMailMessage(); + msg.setFrom(from); + msg.setTo(to); + msg.setSubject(subject); + msg.setText(content); + try { + // 发送消息 + this.qqMailSender.send(msg); + System.out.println("发送邮件成功"); + } catch (MailException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + + /** + * 发送带附件的邮件 + */ + public void sendEmailWithAttachments(String from, String authWord, String to, + String subject, String content, Map files) { + try { + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + MimeMessage message = qqMailSender.createMimeMessage(); + // 需要指定第二个参数为true 代表创建支持可选文本,内联元素和附件的多部分消息 + MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content); + // 传入附件 + for (Map.Entry entry : files.entrySet()) { + helper.addAttachment(entry.getKey(), entry.getValue()); + } + // 发送消息 + this.qqMailSender.send(message); + System.out.println("发送邮件成功"); + } catch (MessagingException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + + + /** + * 发送带内嵌资源的邮件 + */ + public void sendEmailWithInline(String from, String authWord, String to, + String subject, String content, File file) { + try { + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + MimeMessage message = qqMailSender.createMimeMessage(); + // 需要指定第二个参数为true 代表创建支持可选文本,内联元素和附件的多部分消息 + MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + // 使用true标志来指示包含的文本是HTML 固定格式资源前缀 cid: + helper.setText("", true); + // 需要先指定文本 再指定资源文件 + FileSystemResource res = new FileSystemResource(file); + helper.addInline("image", res); + // 发送消息 + this.qqMailSender.send(message); + System.out.println("发送邮件成功"); + } catch (MessagingException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + + /** + * 使用模板邮件 + */ + public void sendEmailByTemplate(String from, String authWord, String to, + String subject, String content) { + try { + Template t = groupTemplate.getTemplate("template.html"); + t.binding("subject", subject); + t.binding("content", content); + String text = t.render(); + // 设置发送人邮箱和授权码 + qqMailSender.setUsername(from); + qqMailSender.setPassword(authWord); + // 实例化消息对象 + MimeMessage message = qqMailSender.createMimeMessage(); + // 指定 utf-8 防止乱码 + MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + // 为true 时候 表示文本内容以 html 渲染 + helper.setText(text, true); + this.qqMailSender.send(message); + System.out.println("发送邮件成功"); + } catch (MessagingException ex) { + // 消息发送失败可以做对应的处理 + System.err.println("发送邮件失败" + ex.getMessage()); + } + } + +} diff --git a/spring/spring-email/src/main/resources/beetl/template.html b/spring/spring-email/src/main/resources/beetl/template.html new file mode 100644 index 0000000..f8245f6 --- /dev/null +++ b/spring/spring-email/src/main/resources/beetl/template.html @@ -0,0 +1,10 @@ + + + + + + +

邮件主题: ${subject}

+

${content}

+ + \ No newline at end of file diff --git a/spring/spring-email/src/main/resources/springApplication.xml b/spring/spring-email/src/main/resources/springApplication.xml new file mode 100644 index 0000000..cf5b2d6 --- /dev/null +++ b/spring/spring-email/src/main/resources/springApplication.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring/spring-email/src/test/java/com/heibaiying/SendEmail.java b/spring/spring-email/src/test/java/com/heibaiying/SendEmail.java new file mode 100644 index 0000000..da654cd --- /dev/null +++ b/spring/spring-email/src/test/java/com/heibaiying/SendEmail.java @@ -0,0 +1,59 @@ +package com.heibaiying; + +import com.heibaiying.email.SpringMail; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * @author : heibaiying + * @description : 发送邮件测试类 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration({"classpath:springApplication.xml"}) +public class SendEmail { + + @Autowired + private SpringMail springMail; + + // 发送方邮箱地址 + private static final String from = "发送方邮箱地址@qq.com"; + // 发送方邮箱地址对应的授权码 + private static final String authWord = "授权码"; + // 接收方邮箱地址 + private static final String to = "接收方邮箱地址@qq.com"; + + @Test + public void sendMessage() { + + springMail.sendTextMessage(from, authWord, to, "spring简单邮件", "Hello Spring Email!"); + } + + + @Test + public void sendComplexMessage() { + Map fileMap = new HashMap<>(); + fileMap.put("image1.jpg", new File("D:\\LearningNotes\\picture\\msm相关依赖.png")); + fileMap.put("image2.jpg", new File("D:\\LearningNotes\\picture\\RabbitMQ模型架构.png")); + springMail.sendEmailWithAttachments(from, authWord, to, "spring多附件邮件" + , "Hello Spring Email!", fileMap); + } + + @Test + public void sendEmailWithInline() { + springMail.sendEmailWithInline(from, authWord, to, "spring内嵌资源邮件" + , "Hello Spring Email!", new File("D:\\LearningNotes\\picture\\RabbitMQ模型架构.png")); + } + + @Test + public void sendEmailByTemplate() { + springMail.sendEmailByTemplate(from, authWord, to, + "spring模板邮件", "Hello Spring Email!"); + } +}