springboot整合email:
springboot整合email很簡單:
第一步:添加依賴:
第二步:配置文件配置:
有兩種一種網易的一種qq的(注意是企業用還是個人用。不注意會出錯)【smtp.exmail.qq.com 企業用 | smtp.qq.com 個人用】
我只配置個人用的:配置如下:
注意:qq的授權碼是發短信生成的,126的是自己設置的
第三步:email的實現方法:
package com.huhy.demo.email; 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.Service; import javax.mail.internet.MimeMessage; import java.io.File; /** * @author : huhy on 2018/8/16. * @Project_name:springboot_self_gitlab * @LOCAL:com.huhy.demo.email * @description:springboot整合email */ @Service public class MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender sender; @Value("${spring.mail.username}") private String from; /** * 發送純文本的簡單郵件 * @param to * @param subject * @param content */ 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 { sender.send(message); logger.info("測試郵件。"); } catch (Exception e) { logger.error("測試郵件發生異常!", e); } } /** * 發送html格式的郵件 * @param to * @param subject * @param content */ public void sendHtmlMail(String to, String subject, String content){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創建一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); sender.send(message); logger.info("html郵件已經發送。"); } catch (Exception e) { logger.error("發送html郵件時發生異常!", e); } } /** * 發送帶附件的郵件 * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentsMail(String to, String subject, String content, String filePath){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創建一個multipart message 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); sender.send(message); logger.info("帶附件的郵件已經發送。"); } catch (Exception e) { logger.error("發送帶附件的郵件時發生異常!", e); } } /** * 發送嵌入靜態資源(一般是圖片)的郵件 * @param to * @param subject * @param content 郵件內容,需要包括一個靜態資源的id, * @param rscPath 靜態資源路徑和文件名 * @param rscId 靜態資源id */ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創建一個multipart message 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); sender.send(message); logger.info("嵌入靜態資源的郵件已經發送。"); } catch (Exception e) { logger.error("發送嵌入靜態資源的郵件時發生異常!", e); } } }
第四步:單元測試:
1 package com.huhy.demo; 2 3 import com.huhy.demo.email.MailService; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.test.context.SpringBootTest; 8 import org.springframework.test.context.junit4.SpringRunner; 9 10 import java.util.HashMap; 11 import java.util.Map; 12 13 @RunWith(SpringRunner.class) 14 @SpringBootTest 15 public class SpringbootEmailApplicationTests { 16 17 @Test 18 public void contextLoads() { 19 } 20 @Autowired 21 private MailService mailService; 22 23 //接收郵箱 24 private String to = "3403903571@qq.com"; 25 26 @Test 27 public void sendSimpleMail() { 28 mailService.sendSimpleMail(to, "主題:簡單郵件", "測試郵件內容"); 29 } 30 31 32 @Test 33 public void sendHtmlMail() { 34 Map<String, Object> model = new HashMap<String, Object>(); 35 model.put("time", System.currentTimeMillis()); 36 model.put("message", "huhy 測試郵件"); 37 String content = "111111111111111111111111111111111"; 38 39 mailService.sendHtmlMail(to, "主題:html郵件", content); 40 } 41 42 @Test 43 public void sendAttachmentsMail() { 44 mailService.sendAttachmentsMail(to, "主題:帶附件的郵件", "有附件,請查收!", "C:\\Users\\Administrator\\Desktop\\1.png"); 45 } 46 47 @Test 48 public void sendInlineResourceMail() { 49 String rscId = "huhy"; 50 mailService.sendInlineResourceMail(to, 51 "主題:嵌入靜態資源的郵件", 52 "<html><body>這是有嵌入靜態資源:<img src=\'cid:" + rscId + "\' ></body></html>", 53 "C:\\Users\\Administrator\\Desktop\\1.png", 54 rscId); 55 } 56 57 58 }