<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] # spring-boot-starter-mail ## 配置 ~~~ <!-- 郵件服務 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- Thymeleaf 模版,用于發送模版郵件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ~~~ 使用郵件服務需要配置自己可以使用的郵箱信息,一般需要配置發送協議 SMTP、郵箱帳號 ~~~ #使用163.com的郵件服務器 spring.mail.host=smtp.163.com spring.mail.port=25 #在163.com注冊的用戶名,注意這里不需要@163.com后綴 spring.mail.username=****** #在163.com注冊的密碼 spring.mail.password=****** spring.mail.default-encoding=UTF-8 ~~~ ## 文本郵件 ~~~ @Service @Slf4j public class MailService { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender mailSender; /** * 發送簡單文本郵件 * * @param to * @param subject * @param content */ public void sendSimpleTextMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(content); message.setFrom(from); mailSender.send(message); log.info("【文本郵件】成功發送!to={}", to); } } ~~~ 創建 Springboot 的單元測試類測試文本郵件,實驗中的收信人為了方便,都設置成了自己的郵箱。 ~~~ @RunWith(SpringRunner.class) @SpringBootTest public class MailServiceTest { @Autowired private MailService mailService; @Autowired private TemplateEngine templateEngine; @Test public void sendSimpleTextMailTest() { String to = "niumoo@126.com"; String subject = "Springboot 發送簡單文本郵件"; String content = "<p>第一封 Springboot 簡單文本郵件</p>"; mailService.sendSimpleTextMail(to, subject, content); } } ~~~ 運行單元測試,測試文本郵件的發送。 **PS:如果運行報出異常`AuthenticationFailedException: 535 Error`. 一般都是用戶名和密碼有誤。** ~~~ Caused by: javax.mail.AuthenticationFailedException: 535 Error: authentication failed at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780) at javax.mail.Service.connect(Service.java:366) at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:517) at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:436) ... 34 more ~~~ 正常運行輸出成功發送的日志。 ~~~ 2019-03-11 23:35:14.743 INFO 13608 --- [ main] n.codingme.boot.service.MailServiceTest : Started MailServiceTest in 3.964 seconds (JVM running for 5.749) 2019-03-11 23:35:24.718 INFO 13608 --- [ main] net.codingme.boot.service.MailService : 【文本郵件】成功發送!to=niumoo@126.com ~~~ 文本郵件正常收到,同時可見文本郵件中的 HTML 標簽也不會被解析。 ## HTML郵件 在上面的`MailService`類里新加一個方法`sendHtmlMail`,用于測試 HTML 郵件。 ~~~ /** * 發送 HTML 郵件 * * @param to * @param subject * @param content * @throws MessagingException */ public void sendHtmlMail(String to, String subject, String content) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(message, true); messageHelper.setFrom(from); messageHelper.setTo(to); messageHelper.setSubject(subject); // true 為 HTML 郵件 messageHelper.setText(content, true); mailSender.send(message); log.info("【HTML 郵件】成功發送!to={}", to); } ~~~ 在測試方法中增加 HTML 郵件測試方法。 ~~~ @Test public void sendHtmlMailTest() throws MessagingException { String to = "niumoo@126.com"; String subject = "Springboot 發送 HTML 郵件"; String content = "<h2>Hi~</h2><p>第一封 Springboot HTML 郵件</p>"; mailService.sendHtmlMail(to, subject, content); } ~~~ 運行單元測試,查看收信情況。 HTML 郵件正常收到,HTML 標簽也被解析成對應的樣式。 ## 附件郵件 在上面的`MailService`類里新加一個方法`sendAttachmentMail`,用于測試 附件郵件。 ~~~ /** * 發送帶附件的郵件 * * @param to * @param subject * @param content * @param fileArr */ public void sendAttachmentMail(String to, String subject, String content, String... fileArr) throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(content, true); // 添加附件 for (String filePath : fileArr) { FileSystemResource fileResource = new FileSystemResource(new File(filePath)); if (fileResource.exists()) { String filename = fileResource.getFilename(); messageHelper.addAttachment(filename, fileResource); } } mailSender.send(mimeMessage); log.info("【附件郵件】成功發送!to={}", to); } ~~~ 在測試方法中增加附件郵件測試方法。 ~~~ @Test public void sendAttachmentTest() throws MessagingException { String to = "niumoo@126.com"; String subject = "Springboot 發送 HTML 附件郵件"; String content = "<h2>Hi~</h2><p>第一封 Springboot HTML 附件郵件</p>"; String filePath = "pom.xml"; mailService.sendAttachmentMail(to, subject, content, filePath, filePath); } ~~~ 帶附件的郵件正常收到,多個附件的實現方式同理。 ## 圖片郵件 圖片郵件和其他的郵件方式略有不同,圖片郵件需要先在內容中定義好圖片的位置并出給一個記錄 ID ,然后在把圖片加到郵件中的對于的 ID 位置。 在上面的`MailService`類里新加一個方法`sendImgMail`,用于測試 附件郵件。 ~~~ /** * 發送帶圖片的郵件 * * @param to * @param subject * @param content * @param imgMap */ public void sendImgMail(String to, String subject, String content, Map<String, String> imgMap) throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(content, true); // 添加圖片 for (Map.Entry<String, String> entry : imgMap.entrySet()) { FileSystemResource fileResource = new FileSystemResource(new File(entry.getValue())); if (fileResource.exists()) { String filename = fileResource.getFilename(); messageHelper.addInline(entry.getKey(), fileResource); } } mailSender.send(mimeMessage); log.info("【圖片郵件】成功發送!to={}", to); } ~~~ 在測試方法中增加圖片郵件測試方法,測試方法中使用的 apple.png 是項目里的一個圖片。 ~~~ @Test public void sendImgTest() throws MessagingException { String to = "niumoo@126.com"; String subject = "Springboot 發送 HTML 圖片郵件"; String content = "<h2>Hi~</h2><p>第一封 Springboot HTML 圖片郵件</p><br/><img src=\"cid:img01\" /><img src=\"cid:img02\" />"; String imgPath = "apple.png"; Map<String, String> imgMap = new HashMap<>(); imgMap.put("img01", imgPath); imgMap.put("img02", imgPath); mailService.sendImgMail(to, subject, content, imgMap); } ~~~ 兩個圖片正常顯示在郵件里。 ## 模版郵件 模版郵件的用處很廣泛,像經常收到的注冊成功郵件或者是操作通知郵件等都是模版郵件,模版郵件往往只需要更改其中的幾個變量。Springboot 中的模版郵件首選需要選擇一款模版引擎,在引入依賴的時候已經增加了模版引擎 `Thymeleaf`. 模版郵件首先需要一個郵件模版,我們在 `Templates` 下新建一個 `HTML` 文件 `RegisterSuccess.html`. 其中的 username 是給我們自定義的。 ~~~ <!DOCTYPE html> <html lang="zh-CN" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>注冊成功通知</title> </head> <body> <p>[[${username}]],您好! </p> <p> 新的公鑰已添加到你的賬戶:<br/> 標題: HP-WIN10 <br/> 如果公鑰無法使用,你可以在這里重新添加: SSH Keys </p> </body> </html> ~~~ 在郵件服務`MailService`中注入模版引擎,然后編寫郵件模版發送代碼。 ~~~ @Autowired private TemplateEngine templateEngine; /** * 發送模版郵件 * * @param to * @param subject * @param paramMap * @param template * @throws MessagingException */ public void sendTemplateMail(String to, String subject, Map<String, Object> paramMap, String template) throws MessagingException { Context context = new Context(); // 設置變量的值 context.setVariables(paramMap); String emailContent = templateEngine.process(template, context); sendHtmlMail(to, subject, emailContent); log.info("【模版郵件】成功發送!paramsMap={},template={}", paramMap, template); } ~~~ 在單元單元測試中增加模版郵件測試方法,然后發送郵件測試。 ~~~ @Test public void sendTemplateMailTest() throws MessagingException { String to = "niumoo@126.com"; String subject = "Springboot 發送 模版郵件"; Map<String, Object> paramMap = new HashMap(); paramMap.put("username", "Darcy"); mailService.sendTemplateMail(to, subject, paramMap, "RegisterSuccess"); } ~~~ ## 補充 上面的例子中,是 Springboot 郵件服務的基本用法,代碼也有很多重復,和實際的使用情況相比還有很多不足,比如缺少`異常處理機制`,在發送失敗時的`重試機制`也沒有,實際情況中郵件服務往往對實時性不高,多說情況下會用于`異步請求`
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看