ARTICLE AD BOX
I am trying to send email using Spring Boot with Gmail SMTP, but I am getting the following exception:
org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: jakarta.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketException: Connection resetapplication.yml:
spring: mail: host: smtp.gmail.com port: 587 username: [email protected] password: your_app_password properties: mail: smtp: auth: true starttls: enable: true required: true connectiontimeout: 5000 timeout: 5000 writetimeout: 5000 debug: trueMail service code:
private final JavaMailSender javaMailSender; @Value("${app.mail.from}") private String fromEmail; public void sendMail(EmailRequest request) { try { System.out.println("From mail spotted -> " + fromEmail); SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(fromEmail); message.setTo(request.getTo().toArray(new String[0])); if (request.getCc() != null && !request.getCc().isEmpty()) { message.setCc(request.getCc().toArray(new String[0])); } if (request.getBcc() != null && !request.getBcc().isEmpty()) { message.setBcc(request.getBcc().toArray(new String[0])); } message.setSubject(request.getSubject()); message.setText(request.getBody()); javaMailSender.send(message); log.info("Simple email sent successfully to: {}", request.getTo()); } catch (Exception e) { log.error("error exception while sendmail", e); throw new RuntimeException(e); } }Full error:
2026-05-18T10:07:26.234+05:30 ERROR 2684 --- [SERVICEB] [nio-9898-exec-4] c.t.s.modules.mail.service.MailService : error exception while sendmail org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: jakarta.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketException: Connection resetWhat I have already checked:
SMTP host is smtp.gmail.com
Port is 587
STARTTLS is enabled
Internet connection is working
Why am I getting java.net.SocketException: Connection reset while sending email using Gmail SMTP in Spring Boot?
Do I need to use Gmail App Passwords or change any SMTP configuration?
