使用多个发射器发送电子邮件

时间:2016-12-22 10:22:32

标签: java spring email spring-boot

我尝试使用spring boot发送电子邮件。所以我在pom.xml中添加了这个启动器

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

在我的application.yml

spring:
 mail:
    host: smtp.gmail.com
    username: xxx@gmail.com
    password: xxxx
    port: 587
    properties:
      mail:
        smtp:
          starttls:
            enable: true
          auth: true 

这是我用来发送电子邮件的方法

private EmailStatus sendM(String to, String subject, String text, Boolean isHtml) {
        try {
            MimeMessage mail = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);

             helper.setFrom("yyy@gmail.com");   // it doesn't work
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, isHtml);
            javaMailSender.send(mail);
            LOGGER.info("Send email '{}' to: {}", subject, to);
            return new EmailStatus(to, subject, text).success();
        } catch (Exception e) {
            LOGGER.error(String.format("Problem with sending email to: {}, error message: {}", to, e.getMessage()));
            return new EmailStatus(to, subject, text).error(e.getMessage());
        }
    }

一切正常,问题是我无法使用方法&#34; setFrom&#34;电子邮件。我收到电子邮件时,它始终是我在application.yml&#34; xxx@gmail.com"中设置的电子邮件。您是否有任何想法如何在我的application.yml中使用多个spring.mail.user?

0 个答案:

没有答案