简体   繁体   中英

Spring: Send HTML mail

I want to create a service to send an HTML mail using Spring email with MimeMessage . Here is my service:

    public void sendHtmlEmail(String receiver,String Subject, String htmlBody) throws MessagingException {

            MimeMessage msg = javaMailSender.createMimeMessage();

            // true = multipart message
            MimeMessageHelper helper = new MimeMessageHelper(msg, false);

            helper.setTo(receiver);

            helper.setSubject(Subject);

            // true = text/html
            helper.setText(htmlBody, true);


            javaMailSender.send(msg);
        }

The problem is that I don't receive an email in html but tags in html,knowing that I put true in the method setText() ! the email I send is displayed in plain html text like following

<html><h1>some text !</h1></html>

some links that could help you:

https://mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/

https://www.baeldung.com/spring-email

here's the application.properties:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password

# Other properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable=true

and the controller

@PostMapping("/htmlMail")
    public String sendHtmlMail(@RequestBody MailDTO mail) {
        mailService.sendHtmlEmail(mail.getReceiver(),mail.getSubject(),mail.getHtmlbody());
        return "html E-Mail Sent ! ";
    }

Sometime required parameter is not there so it gives an error. In your question not whole code or error snep is given so i describe on my way.

Check following two points first,

  1. All required configuration data are provided.
  2. Your email id must have permission to send mail using your application (Allow "less secure app" in your gmail account - if you're using).

Yml Properties file

mail:
    host: smtp.gmail.com                   // Take based on your mail provider
    port: 587
    username: *@gmail.com
    password: ****
    transport:
      protocol: smtp
    properties:
      test-connection: true
      debug: true
      smtp:
        auth: true
        starttls:
          enable: true
          required: true
        ssl:
          enable: true

Code Snippet

   @Autowired
   JavaMailSender mailSender;

   public void sendMail(String to, String subject, String content, boolean 
    isMultipart, boolean isHtml){

      MimeMessage mimeMessage = mailSender.createMimeMessage();

      JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
      mailSender.setHost(EMAIL_HOST);
      mailSender.setPort(EMAIL_PORT);
      mailSender.setUsername(EMAIL_USERNAME);
      mailSender.setPassword(EMAIL_PASSWORD);

      Properties properties = mailSender.getJavaMailProperties();
      properties.put("mail.smtp.starttls.enable", Boolean.TRUE);
      properties.put("mail.transport.protocol", "smtp");
      properties.put("mail.smtp.auth", Boolean.TRUE);
      properties.put("mail.smtp.starttls.required", Boolean.TRUE);
      properties.put("mail.smtp.ssl.enable", Boolean.FALSE);
      properties.put("mail.test-connection", Boolean.TRUE);
      properties.put("mail.debug", Boolean.TRUE);

      mailSender.setJavaMailProperties(properties);

      try {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, isMultipart, "UTF-8");
        messageHelper.setFrom(USER_EMAIL);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, isHtml);
        mailSender.send(mimeMessage);
      } catch (Exception ex) {
        log.warn("Email could not be sent to user '{}': {}", to, ex.getMessage());
      }
    }

For Call

    @Async
    public void sendTestingMail(String mail) {
      String subject = "Test mail from Project Management System";
      String content = "<h1>Be happy, Enjoy Life...!!!</h1>";
      sendMail(mail, subject, content, false, true);
    }

Email kind of configuration you set up once and use multiple place so try to do single full setup.

Have a nice day...!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM