1

I want to send emails using gmail smtp server and spring mail sender, it doesn' throw any exceptions but my mail isn't sent i don't receive it.

Here's how Service looks:

@Service
public class MailSenderService {

    @Autowired
    public MailSender mailSender;

    public void prepareAndSend(String recipient, String message) {
        try {

            SimpleMailMessage mail = new SimpleMailMessage();
            String from = "testSender1@gmail.com";
            String to = "testRecipient1@gmail.com";
            String subject = "Test subject";
            mail.setFrom(from);
            mail.setTo(recipient);
            mail.setSubject(subject);
            mail.setText(message);
            mailSender.send(mail);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

AppProperties

spring.mail.host = smtp.gmail.com
spring.mail.username = ********@gmail.com
spring.mail.password = ********
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.properties.mail.smtp.ssl.enable = true

And pom dependency

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

I deploy my app on port 8080 and just call this service with these two parameters, no exception is caught but in my testRecipient1 inbox i don't find any new mail, what have i missed ?

DanJo
  • 83
  • 1
  • 9
  • Did you configure your sender email account to [allow less secure apps as mentioned here](http://stackoverflow.com/a/32457468/205233)? – Filburt Apr 23 '17 at 11:31
  • Did you check this [Spring Boot - Could not connect to SMTP host: smtp.gmail.com, port: 25, response: 421](http://stackoverflow.com/questions/28064904/spring-boot-could-not-connect-to-smtp-host-smtp-gmail-com-port-25-response). May be helpful. – Nirankumar Kotteswaradu Apr 23 '17 at 11:38
  • Well, apart that you should use a logger for errors and not use the printStackTrace() method, did you made some debug? If you didn't, how can you state that everything goes well? – JeanValjean Apr 23 '17 at 19:36

3 Answers3

2

You could also try the below settings but i couldn't give more information regarding this as it will be a long post. However GeeksForGeeks do explain what everything does in the spring mail properties, also google has it's own guidelines which you can find here

p.s. I have also disabled the Access to less secure applications from the gmail account settings

google profile security settings

Below is application.properties

spring.mail.host=smtp.gmail.com
spring.mail.port=465
spring.mail.username=xxxxx@gmail.com
spring.mail.password=*****
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=UTF-8

Below is my email service class (not that professional but for a personal projects it does the trick I need)

@Service
@AllArgsConstructor
@Slf4j
public class EmailService implements EmailSender {

    private final JavaMailSender mailSender;

    @Override
    @Async
    public void send(String to, String email) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
            helper.setText(email, true);
            helper.setTo(to);
            helper.setSubject("Your subject");
            helper.setFrom("xxxxxx@gmail.ro");
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.error("failed to send email", e);
            throw new IllegalStateException("failed to send email");
        }
    }
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
gabriel
  • 33
  • 5
0

If You did not use SSL Certificate in You web application than try my code, it work perfectly in spring boot, text mail and HTML Mail both working perfectly.

@Autowired
private JavaMailSender mailSender;

@RequestMapping(value="/ShareVecancy",method=RequestMethod.GET)
public ModelAndView sendEmailToReference(HttpServletRequest request,HttpSession session){

    try {

            MimeMessage mail = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mail, true);
            messageHelper.setTo("reciverEmail@mail.com");
            messageHelper.setSubject("Testing mail");
            messageHelper.setText("HTML Text or Any text You want to send ", true);
            mailSender.send(mail);

        } catch (MailException e) {
            e.printStackTrace();
        }


}

Property File:::

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=xyz@gmail.com
spring.mail.password=*******
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.default-encoding=UTF-8

Dependency ::

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

application.yml example (tested with Spring Boot 2.1.1):

spring
  mail:
    host: smtp.gmail.com
    username: youremail@gmail.com
    port: 587
    password: ******
    protocol: smtp
    properties:
      mail:
        smtp:
          starttls:
            enable: true
jtim
  • 246
  • 3
  • 10
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Alex Riabov Dec 03 '18 at 20:21