javaspring-bootsmtpgmailgmail-imap

sending email from SpringBoot 2.1.3.RELEASE


I want to send an email from SpringBoot 2.1.3.RELEASE; I have defibed those properties:

spring.mail.host=smtp.gmail.com
spring.mail.username=nunito.calzada@gmail.com
spring.mail.password=aMdwd3cded2@
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.propertirs.mail.smtp.ssl.enable = true
and using org.springframework.mail.MailSender

I am sending the email using org.springframework.mail.MailSender

    mailSender.send(mailMessage);

Everything seems to be OK, I don't see any exception, any error, but I don't receive email, not even in the SPAM email

I also tried

spring.mail.properties.mail.smtp.socketFactory.port = 587

with the same result


Solution

  • for Spring Boot add the dependency

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

    Once the dependency is in place, the next step is to specify the mail server properties in the application.properties file using the spring.mail.* namespace.

    For example, the properties for Gmail SMTP Server can be specified as:

    spring.mail.protocol=smtp
    spring.mail.host: smtp.gmail.com
    spring.mail.port: 465
    spring.mail.username: <user name>
    spring.mail.password: <password>
    spring.mail.properties.mail.smtp.auth: true
    spring.mail.properties.mail.smtp.starttls.enable: true
    mail.smtp.starttls.enable=false
    spring.mail.properties.mail.smtp.starttls.required: true
    spring.mail.properties.mail.smtp.ssl.enable = true
    spring.mail.test-connection=
    

    CODE:

    @Autowired
    private JavaMailSender 
    private void setMailDetailsForSend( final String payload, final String email ) throws MessagingException
    {
    
        final MimeMessage mail = mailSender.createMimeMessage();
        final MimeMessageHelper helper = new MimeMessageHelper( mail, true );
        helper.setTo( email );
        helper.setSubject( "Notification" );
        helper.setText( "text/html", payload );
        mailSender.send( mail );
    
    }
    

    Some SMTP servers require a TLS connection, so the property spring.mail.properties.mail.smtp.starttls.enable is used to enable a TLS-protected connection.