溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用spring mail如何發送一個html郵件

發布時間:2020-12-04 17:27:52 來源:億速云 閱讀:235 作者:Leah 欄目:編程語言

本篇文章為大家展示了利用spring mail如何發送一個html郵件,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

maven

    <!-- email -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

發送圖片

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);
  }

測試

發送實例

    ClassPathResource classPathResource = new ClassPathResource("image_2.png");
    Map<String,Object> att = new HashMap<>();
    att.put("image",classPathResource);
    String content = "<html>
              <body>
                <h5>spring mail發送實例</h5>
                <img src='cid:image'/><br>
              </body>
             </html>";
    try{
      mailService.send(new String[]{"xxxxx@163.com"},"spring mail發送實例",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&#63;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&#63;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)

錯誤碼554

554 DT:SPM 發送的郵件內容包含了未被許可的信息,或被系統識別為垃圾郵件。請檢查是否有用戶發送病毒或者垃圾郵件;

被網易郵箱識別為垃圾郵件了,有個歪招,就是把發送郵箱添加到cc里頭

helper.setCc(from);

上述內容就是利用spring mail如何發送一個html郵件,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女