Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email via Spring boot "spring-boot-starter-mail"

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)

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:

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

And My code:

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

like image 372
krmanish007 Avatar asked Oct 19 '15 11:10

krmanish007


People also ask

How do I automatically send email per day in spring boot?

You need to use Scheduler in your spring boot application to schedule your email task. The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks with the TaskExecutor and TaskScheduler interfaces, respectively.

What is SMTP in spring boot?

Simple mail transfer protocol (SMTP) is a standard communication protocol that transfers mail electronically. SMTP makes it possible to send mail messages from within applications. In this tutorial, we will be using SMTP with Spring Boot to send mail messages from our application.


1 Answers

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

Your example would be:

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

With the following 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

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.

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

like image 174
JeanValjean Avatar answered Oct 14 '22 19:10

JeanValjean