Cannot connect Spring Boot app to AWS SES via SMTP: MailConnectException: Couldn't connect to host

Viewed 199

I try to configure my app to send registration confirmation emails using AWS SES.

I have this error:

Feb  6 15:23:18 ip-172-31-8-145 web: 2022-02-06 15:23:18.334  WARN 17358 --- [nio-8080-exec-7] com.....app.service.MailService    : Email could not be sent to user 'name@gmail.com': Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: email-smtp.us-east-1.amazonaws.com, 25; timeout -1;

I try to use the SMTP protocol. For now, my domain is verified, and the SES account is approved for production (moved from the sandbox).

In my application.properties I have the following settings:

spring.mail.host=email-smtp.us-east-1.amazonaws.com
spring.mail.port=25

I created the SMTP credentials and set them as environment variables in AWS:

SPRING_MAIL_USERNAME = value
SPRING_MAIL_PASSWORD = password

I also have verified the MAIL FROM domain in the format: mail.company.com.

I don't understand which email address AWS will use to send emails. I need it to set up in message.setFrom("mail@company.com"); line of my java service as sender email.

package com.vega.springit.service;

import com.vega.springit.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.util.Locale;

@Service
@Slf4j
public class MailService {

    private final JavaMailSender javaMailSender;
    private final SpringTemplateEngine templateEngine;
    private final String BASE_URL = "http://localhost:8080";

    public MailService(JavaMailSender javaMailSender, SpringTemplateEngine templateEngine) {
        this.javaMailSender = javaMailSender;
        this.templateEngine = templateEngine;
    }

    @Async
    public void sendEmail(String to, String subject, String content, boolean isMultiPart, boolean isHtml) {
        log.debug("Sending Email");

        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
            message.setTo(to);
            message.setFrom("mail@mail.company.com");  // <- need to put email address there
            message.setSubject(subject);
            message.setText(content, isHtml);
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
        }
    }

    @Async
    public void sendEmailFromTemplate(User user, String templateName, String subject) {
        Locale locale = Locale.ENGLISH;
        Context context = new Context(locale);
        context.setVariable("user", user);
        context.setVariable("baseURL", BASE_URL);
        String content = templateEngine.process(templateName, context);
        sendEmail(user.getEmail(), subject, content, false, true);
    }

    @Async
    public void sendActivationEmail(User user) {
        log.debug("Sending activation email to '{}'", user.getEmail());
        sendEmailFromTemplate(user, "email/activation", "Springit User Activation");
    }

    @Async
    public void sendWelcomeEmail(User user) {
        log.debug("Sending activation email to '{}'", user.getEmail());
        sendEmailFromTemplate(user, "email/welcome", "Welcome new Springit User");
    }

}

I tried the same without a custom domain, but just using @gmail email. Now I tried it with a custom domain. It didn't work. In both cases, I had the same error.

Please help me to figure out what I have to do to finish these settings and make the service work. What can be the problem with connection? From which email AWS SES is going to send emails in my case? Do I need to do any extra settings: create an email with my domain, set up subscriptions on topics, etc?

Thanks in advance.

0 Answers
Related