Send html mail using spring mail

  java

Order

This article shows how to use spring mail to send html mail.

maven

        <!-- email -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

Send picture

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);
        //support html
        helper.setText(text, true); 

        // 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);
    }

test

  • Send instance
        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();
        }
  • abnormal
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)

Error code 554

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

Netease email identified as spam, there is a trick, is to add the sending email to cc

        helper.setCc(from);

doc