2016-06-02 3 views
1

Я пытаюсь отправить почту с Hotmail на Yahoo/Gmail, используя код, как показано ниже с smtp.live.com или smtp-mail.outlook.com, но получаю сообщение об ошибке:Почему не удается отправить почту через smtp-сервер?

String to = SendTo.getText(); 
String from = username; 

     java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
     Properties properties = System.getProperties(); 
     properties.put("mail.smtp.ssl.enable","true"); 
     properties.put("mail.smtp.host", "smtp.live.com"); // i try smtp-mail.outlook.com also cant 
     properties.put("mail.smtp.port", 465); 
     properties.put("mail.smtp.auth", "true"); 


     Authenticator authenticator = new Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username,passwd); 
    }}; 

     Session session = Session.getInstance(properties,authenticator); //default session 

     if(attachment.getText().isEmpty()){ 
       try{ 
      MimeMessage message = new MimeMessage(session); //email message 
      message.setFrom(new InternetAddress(from));  //setting header fields 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
      message.setSubject(subjectField.getText()); //subject line 

      //mail body 
      message.setText(TextArea.getText()); 
      Transport transport = session.getTransport("smtp"); 
      transport.connect(host, from, to); 
      //host = smtp.live.com [email protected] [email protected] 
      transport.sendMessage(message, message.getAllRecipients()); 
      transport.close(); 
      System.out.println("Sent message successfully...."); 

ОШИБКА:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 465; 
    nested exception is: 
    java.net.ConnectException: Connection timed out: connect 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) 
    at javax.mail.Service.connect(Service.java:295) 
    at javax.mail.Service.connect(Service.java:176) 
+0

Пожалуйста, переформатируйте исключение, чтобы поддерживать разрывы строк и упростить чтение. – Neapolitan

+0

решена: для использования hotmail transport.connect (host, 587, username, passwd); properties.put ("mail.smtp.starttls.enable", "true"); – javafreak

ответ

0

ни хозяина smtp.live.com ни хозяина smtp-mail.outlook.com, как представляется, принимать соединения на порт 465.

Вы, вероятно, следует использовать порт 587 в вместо него.

Смежные вопросы