前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用spring mail发送html邮件

使用spring mail发送html邮件

作者头像
code4it
发布2018-09-17 15:00:00
1.7K0
发布2018-09-17 15:00:00
举报
文章被收录于专栏:码匠的流水账

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

maven

代码语言:javascript
复制
        <!-- email -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

发送图片

代码语言:javascript
复制
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);
    }

测试

  • 发送实例 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> ![](cid:image)<br> </body> </html>"; try{ mailService.send(new String[]{"xxxxx@163.com"},"spring mail发送实例",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) 错误码554 554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件; 被网易邮箱识别为垃圾邮件了,有个歪招,就是把发送邮箱添加到cc里头 helper.setCc(from);

doc

  • springboot发送邮件的实现例子
  • 企业退信的常见问题?
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-09-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven
  • 发送图片
  • 测试
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档