javaspring-bootjakarta-mailmailer

Unexpected error when trying to send a email via spring boot


I needed to write a service for sending emails, but no matter how many sources I looked at java mailer documentation, nothing comes out and crashes with an unexpected error:

"Mail server connection failed. Failed messages: org.eclipse.angus.mail.util .MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;\n nested exception is:\n\tjava.net.ConnectException: Connection timed out: connect".

Please tell me, maybe I overlooked something somewhere. Here is my service, which is embedded in another through DI using the interface, there can be no errors there:

@Service
public class EmailService implements IEmailService{

    private static final Logger log = LoggerFactory.getLogger(EmailService.class);
    private final JavaMailSender _javaMailSender;

    public EmailService(JavaMailSender javaMailSender){
        _javaMailSender=javaMailSender;
    }

    @Value("${spring.mail.username}") private String sender;

    @Override
    public ServiceResponse<String> sendEmail(String recipientEmail, String subject, String message) throws Exception {
            var serviceResponse = new ServiceResponse<String>();
            try {

                MimeMessage messageM = _javaMailSender.createMimeMessage();
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(messageM, true);
                mimeMessageHelper.setFrom(sender);
                mimeMessageHelper.setTo(recipientEmail);
                mimeMessageHelper.setSubject(subject);
                mimeMessageHelper.setText(message);
                _javaMailSender.send(messageM);
                serviceResponse.data=null;
                serviceResponse.success=true;
                serviceResponse.message=String.format("Security code for %s successfully sent to your email.", subject);
            }
            catch (Exception ex) {
                serviceResponse.data=null;
                serviceResponse.success=false;
                serviceResponse.message=ex.getMessage();
            }
            return serviceResponse;
    }

Here is my application.yml:

  mail:
    host: smtp.gmail.com
    username: myemail@gmail.com
    password: **** **** **** ****
    port: 587
    protocol: smtp
    properties:
      "mail.transport.protocol": smtp
      "mail.smtp.auth": true
      "mail.smtp.starttls.enable": true

pom.xml package:

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

expected the message to be sent by email, but instead the above error occurred


Solution

  • The problem was solved by replacing the Internet provider (initially it did not work, because I connected to the Wi-Fi of another network with the patch cord of the current network inserted (I don’t know if it was a problem with Windows or the network card itself, that it could not switch the signal, but after switching ) to another network and completely disconnecting from the previous smtp immediately got up and working)).