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

网友投稿 298 2023-04-04


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

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

maven

org.springframework.boot

spring-boot-starter-mail

发送图片

public void send(String from, String[] toMails, String subject, String text,

Map 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 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 att = new HashMap<>();

att.put("image",classPathResource);

String content = "


";

try{

mailService.send(new String[]{"xxxxx@163.com"},"spring mail发送实例",content,att);

}catch (Exception e){

e.printStackTracehttp://();

}

异常

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 15052KSSTFT30070,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 发http://送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;

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

helper.setCc(from);


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解React Native开源时间日期选择器组件(react
下一篇:详解Spring Boot配置文件之多环境配置
相关文章

 发表评论

暂时没有评论,来抢沙发吧~