java - Failed to connect smtp server -


it showing following error ::

exception in thread "main" java.lang.runtimeexception: javax.mail.messagingexception: not connect smtp host: localhost, port: 587, response: 421 @ sendmail.main(sendmail.java:54) caused by: javax.mail.messagingexception: not connect smtp host: localhost, port: 587, response: 421 @ com.sun.mail.smtp.smtptransport.openserver(smtptransport.java:2088) @ com.sun.mail.smtp.smtptransport.protocolconnect(smtptransport.java:699) @ javax.mail.service.connect(service.java:388) @ javax.mail.service.connect(service.java:246) @ javax.mail.service.connect(service.java:195) @ javax.mail.transport.send0(transport.java:254) @ javax.mail.transport.send(transport.java:124) @ sendmail.main(sendmail.java:49) java result: 1 build successful (total time: 2 seconds)

and code is:

`public class sendmail {      public static void main(string[] args) throws exception{          final string smtp_host = "smtp.gmail.com";         final string smtp_username = "xxx@gmail.com";         final string smtp_password = "xxxxxx";         final string smtp_connection = "tls";           final string toemail="xxx@gmail.com";         final string fromemail="yyy@example.com";          properties props = new properties();         props.put("mail.smtp.auth", "true");         if (smtp_connection.equals("tls")) {                 props.put("mail.smtp.starttls.enable", "true");                 props.put("mail.smtp.port", "587");         } else{                 props.put("mail.smtp.socketfactory.port", "465");                 props.put("mail.smtp.socketfactory.class", "javax.net.ssl.sslsocketfactory");                 props.put("mail.smtp.port", "465");         }          session session = session.getinstance(props,           new javax.mail.authenticator() {                 protected passwordauthentication getpasswordauthentication() {                         return new passwordauthentication(smtp_username, smtp_password);                 }           });          try {             message msg = new mimemessage(session);             msg.setfrom(new internetaddress(fromemail, "noreply"));             msg.addrecipient(message.recipienttype.to,                              new internetaddress(toemail, "mr. recipient"));             msg.setsubject("welcome javamail api");             msg.settext("javamail api test - sending email example through remote smtp server");             transport.send(msg);             system.out.println("email sent successfully...");         } catch (addressexception e) {             throw new runtimeexception(e);         } catch (messagingexception e) {             throw new runtimeexception(e);         }     } }` 

below working code. not setting ssl socket factory in non-tls case.

import java.util.*; import javax.mail.*; import javax.mail.internet.*;  public class email {  private static string user_name = "username";  // gmail user name (just part before "@gmail.com") private static string password = "password"; // gmail password  private static string recipient = "xxxxx@gmail.com";  public static void main(string[] args) {     string = user_name;     string pass = password;     string[] = { recipient }; // list of recipient email addresses     string subject = "java send mail example";     string body = "hi ....,!";      sendfromgmail(from, pass, to, subject, body); }  private static void sendfromgmail(string from, string pass, string[] to, string subject, string body) {     properties props = system.getproperties();   string host = "smtp.gmail.com";      props.put("mail.smtp.starttls.enable", "true");      props.put("mail.smtp.ssl.trust", host);     props.put("mail.smtp.user", from);     props.put("mail.smtp.password", pass);     props.put("mail.smtp.port", "587");     props.put("mail.smtp.auth", "true");       session session = session.getdefaultinstance(props);     mimemessage message = new mimemessage(session);      try {           message.setfrom(new internetaddress(from));         internetaddress[] toaddress = new internetaddress[to.length];          // array of addresses         for( int = 0; < to.length; i++ ) {             toaddress[i] = new internetaddress(to[i]);         }          for( int = 0; < toaddress.length; i++) {             message.addrecipient(message.recipienttype.to, toaddress[i]);         }            message.setsubject(subject);         message.settext(body);           transport transport = session.gettransport("smtp");           transport.connect(host, from, pass);         transport.sendmessage(message, message.getallrecipients());         transport.close();      }     catch (addressexception ae) {         ae.printstacktrace();     }     catch (messagingexception me) {         me.printstacktrace();     }     }    }  

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -