Connection refused.Failed messages: javax.mail.MessagingException: Could not connect

267 Views Asked by At

I have implemented Javamailsender for sending emails. But I am always getting this exception

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: XXXXX, port: 26;

I checked with the command, to see if the port is connected for my VPN

nc -zv XXXXX 26

But still, I get this error. I feel I am missing something in the code, but I don't see anything there too

Here is my code


@Configuration
class MailConfig {

    private static final Logger LOG = LogManager.getLogger(MailConfig.class);

    @Value("${mail-sent-from}")
    private String sentFrom;

    @Value("${mail-copy-to}")
    private String copyTo;

    @Value("${mail-sent-to}")
    private String sentTo;

    @Bean
    public SimpleMailMessage emailTemplate() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sentFrom);
        message.setTo(sentTo);
        if (StringUtils.isNotEmpty(copyTo)) {
            message.setCc(copyTo);
        }
        message.setText("run finished. ");
        return message;
    }
}

@Service
public class MailService {
    private static final Logger LOG = LogManager.getLogger(MailService.class);

    @Autowired
    private JavaMailSender mailSender;

    /**
     * This method will send compose and send the message
     */
    private void sendSimpleMessage(String from, String[] bcc, String[] to, String subject, String messageBody) {
        LOG.debug("Sending email ...");
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setBcc(bcc);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(messageBody);

            mailSender.send(message);
        } catch (MailException e) {
            LOG.error("A problem prevented sending a confirmation {} email to {}", subject, to, e);
        }
    }

    public void sendSimpleMessageUsingTemplate(String subject,
                                               SimpleMailMessage template,
                                                ) {
        String messageBody = String.format(template.getText());
        sendSimpleMessage(template.getFrom(), template.getBcc(), template.getTo(), subject, messageBody);
    }

I have added the properties in property file

# mail properties
spring.mail.host     = XXXXX
spring.mail.port     = 26
spring.mail.username = 
spring.mail.password = 

Later I have called it in the main application


 @Autowired
    private MailService  emailService;

    @Autowired
    private SimpleMailMessage ReportMail;

 emailService.sendSimpleMessageUsingTemplate("Download Run Status Report",
                    downloadsReportMail);

0

There are 0 best solutions below