Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send emails with Spring by using Java annotations

How could I send an email with Spring 4 (and Spring Boot) by using a pure annotation-based approach (according to the Java Configurations rules)?

like image 725
vdenotaris Avatar asked Mar 18 '14 15:03

vdenotaris


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.


2 Answers

A simple solution (where you will be using an SMTP server with no authentication) for configuring the email service would by

@Configuration 
public class MailConfig {

    @Value("${email.host}")
    private String host;

    @Value("${email.port}")
    private Integer port;

    @Bean
    public JavaMailSender javaMailService() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setHost(host);
        javaMailSender.setPort(port);

        javaMailSender.setJavaMailProperties(getMailProperties());

        return javaMailSender;
    }

    private Properties getMailProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.auth", "false");
        properties.setProperty("mail.smtp.starttls.enable", "false");
        properties.setProperty("mail.debug", "false");
        return properties;
    }
}

Spring must be able to resolve the properties email.host and email.port in of the usual ways (in case of Spring Boot the simplest is to put then in application.properties)

In any class that needs the services of JavaMailSender, just inject with one of the usual ways (such as @Autowired private JavaMailSender javaMailSender)


UPDATE

Note that since version 1.2.0.RC1 Spring Boot can auto-configure JavaMailSender for you. Check out this part of the documentation. As you can see from the documentation, almost no configuration is required to get up and running!

like image 148
geoand Avatar answered Sep 29 '22 01:09

geoand


In pom.xml:

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

In application.properties:

spring.mail.host=...
spring.mail.port=...

In Foo.java:

@Component
public class Foo {

    @Autowired
    private JavaMailSender mailSender;

    public void send() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("[email protected]");
        message.setTo("[email protected]");
        message.setSubject("hello");
        mailSender.send(message);
    }
}

Personally, I recommend running a localhost MTA, and using it to relay to your real MTA (like Gmail or SES, or your own). This gives you a "free" asynchronous queue, and centralizes config. I like OpenSMTP.

like image 24
Neil McGuigan Avatar answered Sep 29 '22 01:09

Neil McGuigan