本篇文章為大家展示了利用spring mail如何發(fā)送一個(gè)html郵件,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)主營(yíng)綦江網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開發(fā),綦江h(huán)5成都微信小程序搭建,綦江網(wǎng)站營(yíng)銷推廣歡迎綦江等地區(qū)企業(yè)咨詢
maven
org.springframework.boot spring-boot-starter-mail
發(fā)送圖片
public void send(String from, String[] toMails, String subject, String text, Mapinlines) 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); }
測(cè)試
發(fā)送實(shí)例
ClassPathResource classPathResource = new ClassPathResource("image_2.png"); Mapatt = new HashMap<>(); att.put("image",classPathResource); String content = " spring mail發(fā)送實(shí)例
"; try{ mailService.send(new String[]{"xxxxx@163.com"},"spring mail發(fā)送實(shí)例",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?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)
錯(cuò)誤碼554
554 DT:SPM 發(fā)送的郵件內(nèi)容包含了未被許可的信息,或被系統(tǒng)識(shí)別為垃圾郵件。請(qǐng)檢查是否有用戶發(fā)送病毒或者垃圾郵件;
被網(wǎng)易郵箱識(shí)別為垃圾郵件了,有個(gè)歪招,就是把發(fā)送郵箱添加到cc里頭
helper.setCc(from);
上述內(nèi)容就是利用spring mail如何發(fā)送一個(gè)html郵件,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。