API почты Java, использующийся для отправки электронной почты из программы Java, но показывающий ошибку протокола SMTP - PullRequest
0 голосов
/ 07 мая 2018
public class SendEmail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          String to = "sugandhnikhil@yahoo.com";//change accordingly  
          String from = "sugandh.nikhil@gmail.com";//change accordingly  
          String host = "localhost";//or IP address  

         //Get the session object  
          Properties properties = System.getProperties();  
          properties.setProperty("mail.smtp.host", host);  
          Session session = Session.getDefaultInstance(properties);  

         //compose the message  
          try{  
             MimeMessage message = new MimeMessage(session);  
             message.setFrom(new InternetAddress(from));  
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
             message.setSubject("Ping");  
             message.setText("Hello, this is example of sending email  ");  

             // Send message  
             Transport.send(message);  
             System.out.println("message sent successfully....");  

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

}

Ошибка: Could not connect to SMTP host: localhost, port: 25;

Все классы добавлены соответствующим образом. Я не могу настроить smtp. Я пытался установить много серверов Smtp, но ни один из них не работал.

...