email - Sending mail not send in java -
i trying send mail in java code. fail send email
public static void main(string[] args) { string = "abc@gmail.com"; string = "xtz@gmail.com"; string host = "localhost"; properties properties = system.getproperties(); properties.setproperty("mail.smtp.host", host); session session = session.getdefaultinstance(properties); try { mimemessage message = new mimemessage(session); message.setfrom(new internetaddress(from)); message.addrecipient(message.recipienttype.to, new internetaddress(to)); message.setsubject("this subject"); message.settext("this message body"); transport.send(message); system.out.println("sent message successfully...."); } catch (messagingexception ex) { system.out.println("this exception part >-------->"+ex); } }
but found exception
javax.mail.messagingexception: not connect smtp host: localhost, port: 25; nested exception is: java.net.connectexception: connection refused: connect
it's probable smtp server(even running on computer) not listening on port 25, want try connect different port, example 587.
if port connect not specified in transport property, application try connect port 25 default.
in order specify port different default one, need add following code:
transport transport = session.gettransport("smtp"); transport.connect(smtp_host_name, smtp_port, smtp_from_address, password); transport.sendmessage(msg, msg.getallrecipients()); transport.close();
i facing same problem , turned out port set 26.
apart that, practice, should use:
properties properties = new properties();
and:
session session = session.getinstance(properties);
you should try fix these common mistakes prevent future errors.
Comments
Post a Comment