春季:发送HTML邮件 [英] Spring: Send HTML mail

查看:84
本文介绍了春季:发送HTML邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用Spring电子邮件发送HTML邮件的服务,其中 MimeMessage .这是我的服务:

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-如何发送电子邮件通过-smtp/

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

这是application.properties:

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

和控制器

@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.

首先检查以下两点,

  1. 提供了所有必需的配置数据.
  2. 您的电子邮件ID必须具有使用您的应用程序发送邮件的权限(如果使用的话,请在您的gmail帐户中允许安全性较低的应用程序").


Yml属性文件

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


代码段

   @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());
      }
    }


通话

    @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...!!!

这篇关于春季:发送HTML邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆