springboot傳送郵件的實現例子
阿新 • • 發佈:2018-12-03
springboot是集大成者,對於傳送郵件的功能也不例外。而且往往是原來的功能實現更簡單,方便。更符合Facade Pattern。
下面就是springboot傳送郵件的實現例子,程式碼已封裝好,按照提示的步驟配置好就可以直接用了。當然如果你是新搭的專案,需要引入springboot的mail包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> 引入後它會自動下載javax.mail包 上程式碼: package com.yy.common.utils; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Properties; /** * * @description: 傳送郵箱:可以傳送文字,可以附加html、圖片、附件,支援同時傳送多個郵箱 * 使用: * 第一步:在的專案的配置檔案(application.properties)中加入郵件配置 * spring.mail.host=smtp.163.com * spring.mail.username=***@163.com * spring.mail.password=*** * spring.mail.port=465 * spring.mail.default-encoding=UTF-8 * spring.mail.protocol=smtp * spring.mail.properties.mail.debug=true * spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory * spring.mail.properties.mail.smtp.auth=true * spring.mail.properties.mail.smtp.timeout=25000 * 第二步: * 在你呼叫此工具的類中,加入如下:此處利用springboot的feature * @Resource * private JavaMailSenderImpl mailSender; * * @company:yy * @author: skyler * @time: 2016年8月27日 下午6:05:13 */ public class SendMailUtils { public static final String DEFALUT_ENCODING = "UTF-8"; public static void main(String[] args) throws Exception { JavaMailSenderImpl sender = initJavaMailSender(); String[] ss = { "
[email protected]", "[email protected]" }; sendTextWithHtml(sender, ss, "測試簡單文字郵件傳送! ", " <a href='http://work.dev.gomeplus.com/'>test</a>測試我的簡單郵件傳送機制!! "); //sendTextWithImg(sender, ss, "測試郵件中巢狀圖片!!", "<html><head></head><body><h1>hello 歡迎你!!spring image html mail</h1><img src='cid:image'/></body> , "image", "d:/compare2.png"); //sendWithAttament(sender,"[email protected]","測試郵件中上傳附件!!","<html><head></head><body><h1>你好:附件中有學習資料!</h1></body></html>","c.png","d:/compare2.png"); //sendWithAll(sender, "[email protected]", "測試郵件中巢狀圖片!!", "<html><head></head><body><h1>hello 歡迎你!!spring image html ma ", "image", "d:/compare2.png","工作日誌.docx","d:/工作日誌.docx"); } public static void sendTextWithHtml(JavaMailSenderImpl sender, String[] tos, String subject, String text) throws Exception { MimeMessage mailMessage = sender.createMimeMessage(); initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text); // 傳送郵件 sender.send(mailMessage); System.out.println("郵件傳送成功.."); } public static void sendTextWithImg(JavaMailSenderImpl sender, String[] tos, String subject, String text, String imgId, String imgPath) throws MessagingException { MimeMessage mailMessage = sender.createMimeMessage(); MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text, true, true, "GBK"); // 傳送圖片 FileSystemResource img = new FileSystemResource(new File(imgPath)); messageHelper.addInline(imgId, img); // 傳送郵件 sender.send(mailMessage); System.out.println("郵件傳送成功.."); } public static void sendWithAttament(JavaMailSenderImpl sender, String[] tos, String subject, String text, String AttachName, String filePath) throws MessagingException { MimeMessage mailMessage = sender.createMimeMessage(); MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text, true, true, DEFALUT_ENCODING); FileSystemResource file = new FileSystemResource(new File(filePath)); // 傳送郵件 messageHelper.addAttachment(AttachName, file); sender.send(mailMessage); System.out.println("郵件傳送成功.."); } public static void sendWithAll(JavaMailSenderImpl sender, String[] tos, String from, String subject, String text, String imgId, String imgPath, String AttachName, String filePath) throws MessagingException { MimeMessage mailMessage = sender.createMimeMessage(); MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text, true, true, DEFALUT_ENCODING); // 插入圖片 FileSystemResource img = new FileSystemResource(new File(imgPath)); messageHelper.addInline(imgId, img); // 插入附件 FileSystemResource file = new FileSystemResource(new File(filePath)); messageHelper.addAttachment(AttachName, file); // 傳送郵件 sender.send(mailMessage); System.out.println("郵件傳送成功.."); } private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from, String subject, String text) throws MessagingException { return initMimeMessageHelper(mailMessage, tos, from, subject, text, true, false, DEFALUT_ENCODING); } private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from, String subject, String text, boolean isHTML, boolean multipart, String encoding) throws MessagingException { MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, multipart, encoding); messageHelper.setTo(tos); messageHelper.setFrom(from); messageHelper.setSubject(subject); // true 表示啟動HTML格式的郵件 messageHelper.setText(text, isHTML); return messageHelper; } /** * 這個方法在實際應用中,springboot會通過在配置檔案application.xml * 中加配置自動例項化JavaMailSenderImpl,本方法只是為了測試使用 */ public static JavaMailSenderImpl initJavaMailSender() { Properties properties = new Properties(); properties.setProperty("mail.debug", "true");// 是否顯示除錯資訊(可選) properties.setProperty("mail.smtp.starttls.enable", "false"); properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.auth", "true"); properties.put(" mail.smtp.timeout ", " 25000 "); JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setJavaMailProperties(properties); javaMailSender.setHost("smtp.163.com"); javaMailSender.setUsername("你郵箱的使用者名稱"); // s根據自己的情況,設定username javaMailSender.setPassword("你郵箱的密碼"); // 根據自己的情況, 設定password javaMailSender.setPort(465); javaMailSender.setDefaultEncoding("UTF-8"); return javaMailSender; } }