Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

Tags:

while sending mail I am getting this error

java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

my code is:

Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465");  Session session = Session.getDefaultInstance(props,         new javax.mail.Authenticator() {                 protected PasswordAuthentication getPasswordAuthentication() {                         return new PasswordAuthentication("email","password");                 }         });  try {          Message message = new MimeMessage(session);         message.setFrom(new InternetAddress("email"));         message.setRecipients(Message.RecipientType.TO,                         InternetAddress.parse(this.to));         message.setSubject("Testing");         message.setText("Hey, this is the testing email.");            Transport.send(message); 

Any help would be appreciated.

Thanks in Advance.

like image 776
user1900376 Avatar asked Mar 13 '13 06:03

user1900376


People also ask

How do you fix SMTP error could not connect to SMTP host?

Re: SMTP Error: Could not connect to SMTP host This is to check if your server actually has access to the email server. If it times out then you have a network connectivity problem, a firewall problem or the configuration of the mail server is blocking access.

Can't connect to Gmail SMTP host?

By default, gmail does not allow connections from third party software. If the option “access for less secure apps” is not enabled in the gmail account, users get the error “Unable to connect to SMTP host” in their websites.

Does Gmail use port 465?

The outgoing SMTP server, smtp.gmail.com , requires TLS. Use port 465 , or port 587 if your client begins with plain text before issuing the STARTTLS command.

Why is port 465 blocked?

What's Causing This Error. This error occurs when the SMTP server does not accept connections on port - 465. Port 465 is a deprecated standard for SMTP, so most SMTP servers block connections made using this port.


2 Answers

You need to tell it that you are using SSL:

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 

In case you miss anything, here is working code:

String  d_email = "[email protected]",             d_uname = "Name",             d_password = "urpassword",             d_host = "smtp.gmail.com",             d_port  = "465",             m_to = "[email protected]",             m_subject = "Indoors Readable File: " + params[0].getName(),             m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";     Properties props = new Properties();     props.put("mail.smtp.user", d_email);     props.put("mail.smtp.host", d_host);     props.put("mail.smtp.port", d_port);     props.put("mail.smtp.starttls.enable","true");     props.put("mail.smtp.debug", "true");     props.put("mail.smtp.auth", "true");     props.put("mail.smtp.socketFactory.port", d_port);     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");     props.put("mail.smtp.socketFactory.fallback", "false");      SMTPAuthenticator auth = new SMTPAuthenticator();     Session session = Session.getInstance(props, auth);     session.setDebug(true);      MimeMessage msg = new MimeMessage(session);     try {         msg.setSubject(m_subject);         msg.setFrom(new InternetAddress(d_email));         msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));  Transport transport = session.getTransport("smtps");             transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);             transport.sendMessage(msg, msg.getAllRecipients());             transport.close();          } catch (AddressException e) {             e.printStackTrace();             return false;         } catch (MessagingException e) {             e.printStackTrace();             return false;         } 
like image 92
M. Usman Khan Avatar answered Oct 14 '22 07:10

M. Usman Khan


I have been experiencing this issue when debugging using NetBeans, even executing the actual jar file. Antivirus would block sending email. You should temporarily disable your antivirus during debugging or exclude NetBeans and the actual jar file from being scanned. In my case, I'm using Avast.

See this link on how to Exclude : How to Add File/Website Exception into avast! Antivirus 2014

It works for me.

like image 21
ronIT Avatar answered Oct 14 '22 07:10

ronIT