Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

javax.mail.messagingexception

Himanshu SethiAug 13 2015 — edited Dec 2 2015

I am trying to send mail through my server. I am just writing a simple program but unable to send. I am using javax.mail - 1.5.0.jar. Below is the code:

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

public class SendMailTLS {

  public static void main(String[] args) {

  final String username = "*****@***.com";

  final String password = "*****";

  final String to = "*****@***.com";

  Properties props = new Properties();

  props.put("mail.smtp.auth", "true");

  props.put("mail.smtp.starttls.enable", "true");

  props.put("mail.smtp.host", "**********");

  props.put("mail.smtp.port", "***");

  System.out.println("After putting properties");

  Session session = Session.getInstance(props,

  new javax.mail.Authenticator() {

  protected PasswordAuthentication getPasswordAuthentication() {

  return new PasswordAuthentication(username, password);

  }

  });

  try {

  System.out.println("In try block");

  Message message = new MimeMessage(session);

  message.setFrom(new InternetAddress(username));

  message.setRecipients(Message.RecipientType.TO,

  InternetAddress.parse(to));

  message.setSubject("Testing Subject");

  message.setText("Dear Mail Crawler,"

  + "\n\n No spam to my email, please!");

  Transport.send(message);

  System.out.println("Done");

  } catch (MessagingException e) {

  throw new RuntimeException(e);

  }

  }

}

After running this I am getting following exception:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: *******, port: ***, response: -1

  at SendMailTLS.main(SendMailTLS.java:50)

Caused by: javax.mail.MessagingException: Could not connect to SMTP host: *******, port: ***, response: -1

  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949)

  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)

  at javax.mail.Service.connect(Service.java:367)

  at javax.mail.Service.connect(Service.java:226)

  at javax.mail.Service.connect(Service.java:175)

  at javax.mail.Transport.send0(Transport.java:253)

  at javax.mail.Transport.send(Transport.java:124)

  at SendMailTLS.main(SendMailTLS.java:45)

PLEASE HELP ME IN SOLVING THIS....

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 30 2015
Added on Aug 13 2015
3 comments
2,073 views