通过Spring启动发送电子邮件“spring-boot-starter-mail”

时间:2015-10-19 11:19:24

标签: java spring spring-boot javamail

我正在尝试使用spring boot发送电子邮件,但我得到了:

java.lang.UnsupportedOperationException: Method not yet implemented
        at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:89)
        at org.springframework.mail.javamail.SmartMimeMessage.<init>(SmartMimeMessage.java:52)
        at org.springframework.mail.javamail.JavaMailSenderImpl.createMimeMessage(JavaMailSenderImpl.java:325)

我使用过这个maven条目:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
    </parent>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.2.6.RELEASE</version>
        </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>1.2.6.RELEASE</version>
    </dependency>

application.properties:

spring.mail.host=smtp.gmail.com
spring.mail.port= 25
spring.mail.username= test
spring.mail.password= test

我的代码:

@Autowired
    private JavaMailSender javaMailSender;

private void send() {
        MimeMessage mail = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);
            helper.setTo("mymail@mail.co.uk");
            helper.setReplyTo("someone@localhost");
            helper.setFrom("someone@localhost");
            helper.setSubject("Lorem ipsum");
            helper.setText("Lorem ipsum dolor sit amet [...]");
        } catch (MessagingException e) {
            e.printStackTrace();
        } finally {}
        javaMailSender.send(mail);
        //return helper;
    }

这似乎是一个直截了当但不是我错过了什么!

4 个答案:

答案 0 :(得分:31)

我的建议是使用it.ozimov:spring-boot-email-core库,将所有这些实现隐藏在名为EmailService的单个组件后面 - 好吧,我也在开发库:)。

你的例子是:

@Autowired
public EmailService emailService;

public void sendEmail(){
   final Email email = DefaultEmail.builder()
        .from(new InternetAddress("mymail@mail.co.uk"))
        .replyTo(new InternetAddress("someone@localhost"))
        .to(Lists.newArrayList(new InternetAddress("someone@localhost")))
        .subject("Lorem ipsum")
        .body("Lorem ipsum dolor sit amet [...]")
        .encoding(Charset.forName("UTF-8")).build();

   emailService.send(email);
}

使用以下application.properties

spring.mail.host=your.smtp.com
spring.mail.port=587
spring.mail.username=test
spring.mail.password=test
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

它还支持一些模板引擎,如 Freemarker Mustache Pebble ,同时您可以扩展它以使用更多模板引擎。此外,它还支持电子邮件安排和优先级(例如,密码恢复的高优先级和简报的低优先级。

LinkedIn上有一篇文章解释了如何使用它。它是here

答案 1 :(得分:17)

除了通过javax.mail.internet.MimeMessage引入的版本之外,您在类路径上还有spring-boot-starter-mail的第二个版本。常见的罪魁祸首是Geronimo的JavaMail spec jar。无论它是哪个jar,您都需要将其从应用程序的依赖项中排除。如果您不确定它来自何处,请使用-verbose:class运行您的应用程序。

答案 2 :(得分:1)

不要使用javaMailSender.createMimeMessage(); 尝试使用MimeMessagePreparator&amp;而是MimeMessageHelper

答案 3 :(得分:0)

这对我有用:

    private TemplateEngine templateEngine;
    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    public MailContentBuilder mailContentBuilder;

    public void sendEmail(Users user, VerificationToken verificationToken) throws Exception {
        MimeMessagePreparator messagePreparator = mimeMessage -> {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            String name = user.getFirstname();
            String vtoken = verificationToken.getVtoken();
            String url = "http://3.16.214.183:8888/home/".concat(String.valueOf(user.getUserid())).concat("/").concat(vtoken);
            String content = mailContentBuilder.build(name, url);
            helper.setTo(user.getEmail());
            helper.setSubject("AppName - Please Verify Your Email");
            helper.setText(content, true);
        };
        try {
            mailSender.send(messagePreparator);
        } catch (MailException e) {
            e.printStackTrace();
        }
   }

似乎只需要这种依赖:

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

然后我设置属性:

spring.mail.host=smtp.gmail.com
spring.mail.port=465
spring.mail.username=user.myapp@gmail.com
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false

仅供参考,您选择的端口不是任意的-例如,对于Gmail,必须为465。而且,您还需要对发送Gmail帐户进行更改,以使其正常运行。我很久以前就这样做了,但是我很肯定以前见过这种例外。此示例中使用的lambda可能有助于解决该问题,但不幸的是我不记得了。如果您需要任何澄清或想查看我正在使用的示例的更多代码,请随时与我联系。