词条信息

xiazaiwo
xiazaiwo
进士
词条创建者 发短消息   

相关词条

热门词条

更多>>
什么是端口?到底是做什么的呢?
端口一般指两种,一种是硬件比如路由器或者交换机的插网线的端口,一种是软件的逻辑的概念,比如http的80端口!...
7种进阶方法让你快速测试端口连通性
Ping是Windows、Linux和Unix系统下的一个检查网络连通性的命令工具,对于大部分互联网用户来说很...
电脑开机,总需要按F1,是什么原因造成的?
一.主板掉电这个说法是行业内的叫法了,一般是主板的CMOS电池没电了导致的。也是最常见的一种提示你按F1的提示...
社保降费对个人有什么影响?
下调城镇职工基本养老保险单位缴费比例是政府给企业发的一个大红包,特别是对于企业来说是一个利好,但是对个人来说有...
车辆“出险”对下年保费的影响,到底有多大?
【出险对交强险的影响】【出险对商业险的影响】车辆“出险”对下年保费的影响,到底有多大?这里有必要先提下车险第三...

精选图集

更多>>
简易百科旧版 >>所属分类 >> HTML5   

使用spring mail发送html邮件的示例代码

标签: 暂无标签

顶[0] 发表评论(0) 编辑词条

本文展示一下如何使用spring mail来发送html邮件。

maven

?
1
2
3
4
5
<!-- email -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

发送图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
publicvoidsend(String from, String[] toMails, String subject, String text,
           Map<String,Object> inlines) throwsException{
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = newMimeMessageHelper(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() instanceofClassPathResource){
          helper.addInline(entry.getKey(), (Resource) entry.getValue());
        }
 
      }
    }
 
    mailSender.send(mimeMessage);
  }

测试

发送实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ClassPathResource classPathResource = newClassPathResource("image_2.png");
Map<String,Object> att = newHashMap<>();
att.put("image",classPathResource);
String content = "<html>
          <body>
            <h4>spring mail发送实例</h4>
            <img src='cid:image'/><br>
          </body>
         </html>";
try{
  mailService.send(newString[]{"xxxxx@163.com"},"spring mail发送实例",content,att);
}catch(Exception e){
  e.printStackTrace();
}

异常

?
1
2
3
4
5
6
7
8
9
10
11
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMT<a href="http://www.xiazaiwo.net/softzixun/psshipin/" target="_blank"><u>PS</u></a>endFailedException: 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)

错误码554

554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;

被网易邮箱识别为垃圾邮件了,有个歪招,就是把发送邮箱添加到cc里头

?
1
helper.setCc(from);

 

 

参考资料
[1].  使用spring mail发送html邮件的示例代码   http://www.xiazaiwo.net/tech/kaifa/html/17175.html

附件列表


按字母顺序浏览:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

→我们致力于为广大网民解决所遇到的各种电脑技术问题
 如果您认为本词条还有待完善,请 编辑词条

上一篇B站明确禁止未成年人打赏
下一篇

0
1. 本站部分内容来自互联网,如有任何版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
2. 本站内容仅供参考,如果您需要解决具体问题,建议您咨询相关领域专业人士。
3. 如果您没有找到需要的百科词条,您可以到百科问答提问或创建词条,等待高手解答。

关于本词条的提问

查看全部/我要提问>>