java - Error while sending mails using smtp -


i need send mail gmail account another. used following code.

               string fromaddress = "xxx@gmail.com";      string password = "yyy";     string hostname = "smtp.gmail.com";     string hoststring = "mail.smtp.host";     string toaddress = "yyy@gmail.com";     string emailcontent;      properties properties = system.getproperties();     properties.setproperty(hoststring, hostname);     session session = session.getdefaultinstance(properties);      try{         mimemessage message = new mimemessage(session);         message.setfrom(new internetaddress(fromaddress));         message.addrecipient(message.recipienttype.to, new internetaddress(toaddress));         message.setsubject("hi");         emailcontent = "hi...";         message.settext(emailcontent);         system.out.println(emailcontent);         transport.send(message);         system.out.println("sent....");     }catch (messagingexception mex)      {         mex.printstacktrace();     } 

but error follows... javax.mail.messagingexception: not connect smtp host: smtp.gmail.com, port: 25

how solve this. can please me out.

public static void sendemail(email mail) {      string host = "smtp.gmail.com";     string = "your_gmail_id";     string pass = "your_gmail_password";     properties props = system.getproperties();     props.put("mail.smtp.starttls.enable", "true"); // added line     props.put("mail.smtp.host", 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");      // default session object.     session session = session.getdefaultinstance(props, null);      try {         // create default mimemessage object.         mimemessage message = new mimemessage(session);          // set sender         message.setfrom(new internetaddress("senders_email_id"));          // set recipient         message.addrecipient(message.recipienttype.to, new internetaddress("recipient_email_id"));          // set subject: header field         message.setsubject("subject");          // set content , define type         message.setcontent("content", "text/html; charset=utf-8");          transport transport = session.gettransport("smtp");         transport.connect(host, from, pass);         transport.sendmessage(message, message.getallrecipients());         transport.close();       } catch (messagingexception mex) {         system.out.println(mex.getlocalizedmessage());     }  } 

}`

i think should trick.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -