Search code examples
javajakarta-mail

error while sending mail in java


final int port = 587;
String host = "mail.website.com";  
final String user = "[email protected]";
final String password = "password";  

String to = "[email protected]"; 

Properties props = new Properties();  
props.put("mail.smtp.host", host);  
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port);

Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() {  
        protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication(user, password);  
        }  
    });  

try {  
    MimeMessage message = new MimeMessage(session);  
    message.setFrom(new InternetAddress(user));  
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
    message.setSubject("demo");  
    message.setText("Hello");  

    Transport.send(message);  

    System.out.println("done");  

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

Error:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port:


Solution

  • Your port seems to be closed, are yuu sure is the right one? I bould bet 25 or 465...

    POP3 - port 110
    IMAP - port 143
    SMTP - port 25
    HTTP - port 80
    Secure SMTP (SSMTP) - port 465
    Secure IMAP (IMAP4-SSL) - port 585
    IMAP4 over SSL (IMAPS) - port 993
    Secure POP3 (SSL-POP) - port 995