使用spring mail发送html邮件的示例代码
序
本文展示一下如何使用springmail来发送html邮件。
maven
org.springframework.boot spring-boot-starter-mail
发送图片
publicvoidsend(Stringfrom,String[]toMails,Stringsubject,Stringtext, Mapinlines)throwsException{ MimeMessagemimeMessage=mailSender.createMimeMessage(); MimeMessageHelperhelper=newMimeMessageHelper(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()instanceofClassPathResource){ helper.addInline(entry.getKey(),(Resource)entry.getValue()); } } } mailSender.send(mimeMessage); }
测试
发送实例
ClassPathResourceclassPathResource=newClassPathResource("image_2.png"); Mapatt=newHashMap<>(); att.put("image",classPathResource); Stringcontent=" springmail发送实例
"; try{ mailService.send(newString[]{"xxxxx@163.com"},"springmail发送实例",content,att); }catch(Exceptione){ e.printStackTrace(); }
异常
org.springframework.mail.MailSendException:Failedmessages:com.sun.mail.smtp.SMTPSendFailedException:554DT:SPM126smtp7,DsmowAB3U6X1_LdZjIz+Aw--.26008S31505230070,pleaseseehttp://mail.163.com/help/help_spam_16.htm?ip=123.65.107.103&hostid=smtp7&time=1505230070 ;messageexceptiondetails(1)are: Failedmessage1: com.sun.mail.smtp.SMTPSendFailedException:554DT:SPM126smtp7,DsmowAB3U6X1_LdZjIz+Aw--.26008S31505230070,pleaseseehttp://mail.163.com/help/help_spam_16.htm?ip=123.65.107.103&hostid=smtp7&time=1505230070 atcom.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2267) atcom.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:2045) atcom.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1260) atorg.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448) atorg.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345) atorg.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
错误码554
554DT:SPM发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;
被网易邮箱识别为垃圾邮件了,有个歪招,就是把发送邮箱添加到cc里头
helper.setCc(from);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。