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

2025-05-29 0 29

本文展示一下如何使用html\”>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
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);

}

测试

发送实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
ClassPathResource classPathResource = new ClassPathResource("image_2.png");

Map<String,Object> att = new HashMap<>();

att.put("image",classPathResource);

String content = "<html>

<body>

<h4>spring mail发送实例</h4>

<img src='cid:image'/><br>

</body>

</html>";

try{

mailService.send(new String[]{"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.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)

错误码554

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

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

?

1
helper.setCc(from);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://segmentfault.com/a/1190000011139696

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 使用spring mail发送html邮件的示例代码 https://www.kuaiidc.com/115076.html

相关文章

发表评论
暂无评论