0
final int port = 587;
String host = "mail.website.com";  
final String user = "abc@website.com";
final String password = "password";  

String to = "abc@yourmail.com"; 

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:

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
Aquarius24
  • 1,806
  • 6
  • 33
  • 61
  • I assume you've changed the `host`, `user`, `password` and `to` properties? – Diyarbakir May 11 '16 at 10:26
  • Have a look into the answers of http://stackoverflow.com/questions/20766044/mailconnectexception-while-sending-mail-using-java-mail-api – Sanjit Kumar Mishra May 11 '16 at 10:28
  • See the JavaMail FAQ entry on [debugging connection problems](http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug). Also, you might want to fix these [common JavaMail mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). – Bill Shannon May 11 '16 at 19:07

1 Answers1

0

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 
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109