使用spring mail發(fā)送html郵件的示例代碼
序
本文展示一下如何使用spring mail來(lái)發(fā)送html郵件。
maven
<!-- email -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
發(fā)送圖片
public void send(String from, String[] toMails, String subject, String text,
Map<String,Object> inlines) throws Exception{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
helper.setTo(toMails);
helper.setSubject(subject);
helper.setText(text, true); //支持html
// 增加inline
if(inlines != null){
for(Map.Entry<String,Object> entry: inlines.entrySet()){
if(entry.getValue() instanceof ClassPathResource){
helper.addInline(entry.getKey(), (Resource) entry.getValue());
}
}
}
mailSender.send(mimeMessage);
}
測(cè)試
發(fā)送實(shí)例
ClassPathResource classPathResource = new ClassPathResource("image_2.png");
Map<String,Object> att = new HashMap<>();
att.put("image",classPathResource);
String content = "<html>
<body>
<h4>spring mail發(fā)送實(shí)例</h4>
<img src='cid:image'/><br>
</body>
</html>";
try{
mailService.send(new String[]{"xxxxx@163.com"},"spring mail發(fā)送實(shí)例",content,att);
}catch (Exception e){
e.printStackTrace();
}
異常
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 126 smtp7,DsmowAB3U6X1_LdZjIz+Aw--.26008S3 1505230070,please see http://mail.163.com/help/help_spam_16.htm?ip=123.65.107.103&hostid=smtp7&time=1505230070 ; message exception details (1) are: Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 126 smtp7,DsmowAB3U6X1_LdZjIz+Aw--.26008S3 1505230070,please see http://mail.163.com/help/help_spam_16.htm?ip=123.65.107.103&hostid=smtp7&time=1505230070 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2267) at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:2045) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1260) at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
錯(cuò)誤碼554
554 DT:SPM 發(fā)送的郵件內(nèi)容包含了未被許可的信息,或被系統(tǒng)識(shí)別為垃圾郵件。請(qǐng)檢查是否有用戶發(fā)送病毒或者垃圾郵件;
被網(wǎng)易郵箱識(shí)別為垃圾郵件了,有個(gè)歪招,就是把發(fā)送郵箱添加到cc里頭
helper.setCc(from);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 啟動(dòng)加載數(shù)據(jù) CommandLineRunner的使用
本篇文章主要介紹了Spring Boot 啟動(dòng)加載數(shù)據(jù) CommandLineRunner的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Spring Cloud Zuul添加過(guò)濾器過(guò)程解析
這篇文章主要介紹了Spring Cloud Zuul添加過(guò)濾器過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
MyBatis?Mapper.XML?標(biāo)簽使用小結(jié)
在MyBatis中,通過(guò)resultMap可以解決字段名和屬性名不一致的問(wèn)題,對(duì)于復(fù)雜的查詢,引用實(shí)體或使用<sql>標(biāo)簽可以定義復(fù)用的SQL片段,提高代碼的可讀性和編碼效率,使用這些高級(jí)映射和動(dòng)態(tài)SQL技巧,可以有效地處理復(fù)雜的數(shù)據(jù)庫(kù)交互場(chǎng)景2024-10-10
Mybatis-Plus開(kāi)發(fā)提速器mybatis-plus-generator-ui詳解
這篇文章主要介紹了Mybatis-Plus開(kāi)發(fā)提速器mybatis-plus-generator-ui,本文簡(jiǎn)要介紹一款基于Mybatis-Plus的代碼自助生成器,文章通過(guò)實(shí)例集成的方式來(lái)詳細(xì)講解mybatis-plus-generator-ui,從相關(guān)概念到實(shí)際集成案例,以及具體的擴(kuò)展開(kāi)發(fā)介紹,需要的朋友可以參考下2022-11-11
Spark隨機(jī)森林實(shí)現(xiàn)票房預(yù)測(cè)
這篇文章主要為大家詳細(xì)介紹了Spark隨機(jī)森林實(shí)現(xiàn)票房預(yù)測(cè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
解決Mybatis?mappe同時(shí)傳遞?List?和其他參數(shù)報(bào)錯(cuò)的問(wèn)題
在使用MyBatis時(shí),如果需要傳遞多個(gè)參數(shù)到SQL中,可以遇到參數(shù)綁定問(wèn)題,解決方法包括使用@Param注解和修改mapper.xml配置,感興趣的朋友跟隨小編一起看看吧2024-09-09

