通过Spring引导发送电子邮件

通过Spring引导发送电子邮件

本文介绍了通过Spring引导发送电子邮件“spring-boot-starter-mail”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send emails using spring boot, but am getting:

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条目:

I have used this maven entry:

    <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:

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("[email protected]");
            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;
    }

这看起来很简单,但不是我错过的!

This appears to be a straight forward but don't what am I missing!

推荐答案

我的建议是使用库,将所有这些实现隐藏在名为 EmailService - 好吧,我也在开发图书馆:)。

My recommendation is to use the it.ozimov:spring-boot-email-core library, that hides all these implementations behind a single component called EmailService - well, I'm also developing the library :).

你的例子是:

@Autowired
public EmailService emailService;

public void sendEmail(){
   final Email email = DefaultEmail.builder()
        .from(new InternetAddress("[email protected]"))
        .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 ,同时您可以扩展它以使用更多模板引擎。此外,它还支持电子邮件安排和优先级(例如,密码恢复的高优先级和简报的低优先级。

It also supports some template engines, like Freemarker, Mustache and Pebble, while you can extend it to use more template engines. Moreover, it also supports email scheduling and prioritization (e.g. high priority for password recovery and low priority for newsletter.

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

There is an article on LinkedIn explaining how to use it. It is here.

这篇关于通过Spring引导发送电子邮件“spring-boot-starter-mail”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:12