使用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>
                                ![](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)檢查是否有用戶(hù)發(fā)送病毒或者垃圾郵件;

被網(wǎng)易郵箱識(shí)別為垃圾郵件了,有個(gè)歪招,就是把發(fā)送郵箱添加到cc里頭

        helper.setCc(from);

doc

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,537評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,262評(píng)論 6 342
  • Django 官方文檔 發(fā)送email 官網(wǎng)地址:[https://docs.djangoproject.com/...
    學(xué)以致用123閱讀 2,402評(píng)論 0 2
  • django通過(guò)封裝python的smtplib實(shí)現(xiàn)發(fā)送郵件功能。django 1.11官網(wǎng)翻譯內(nèi)容見(jiàn):http:...
    學(xué)以致用123閱讀 4,105評(píng)論 0 4
  • 數(shù)組 一、相關(guān)概念 1、數(shù)組是值的有序集合。 2、每個(gè)值叫做一個(gè)元素 3、每個(gè)元素在數(shù)組中有一個(gè)位置,以數(shù)字表示。...
    社會(huì)你碼ge閱讀 424評(píng)論 0 0

友情鏈接更多精彩內(nèi)容