Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javamail to connect to Gmail smtp server ignores specified port and tries to use 25

I'm trying to use javamail in a groovy script to send out an email via gmail. I've looked many places online and have been unable to get it working thus far. The error I'm getting when running my script is:

DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false Caught: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25 (javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?)) 

It appears to be trying to use port 25 even though I've specified that it should use port 587. Does anyone know what could be causing this problem, I've used telnet to connect to the smtp server on port 587, and thunderbird uses port 587 with STARTTLS security and is able to successfully send mail using the smtp server. This tells me that it is not a blocked port or connectivity issue. Here is the code I'm using to try and send the email:

import javax.mail.* import javax.mail.internet.*  private class SMTPAuthenticator extends Authenticator {     public PasswordAuthentication getPasswordAuthentication()     {         return new PasswordAuthentication('[email protected]', 'password');     } }  def  d_email = "[email protected]",         d_password = "password",         d_host = "smtp.gmail.com",         d_port  = "587", //465,587         m_to = "[email protected]",         m_subject = "Testing",         m_text = "This is a test."  def 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")  def auth = new SMTPAuthenticator() def session = Session.getInstance(props, auth) session.setDebug(true);  def msg = new MimeMessage(session) msg.setText(m_text) msg.setSubject(m_subject) msg.setFrom(new InternetAddress(d_email)) msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)) Transport.send(msg) 

Any help would be greatly appreciated. Thanks in advance!

-Bryan

like image 977
Bryan Avatar asked Jan 02 '10 02:01

Bryan


People also ask

Can Gmail SMTP use port 25?

On your device or in your app, connect to smtp-relay.gmail.com on one of these ports: 25, 465, or 587.

How can I get SMTP server response using JavaMail?

SMTPTransport t = (SMTPTransport)session. getTransport("smtps"); t. send(message); String response = t. getLastServerResponse(); boolean s = t.

Why is my SMTP for Gmail not working?

In Google Mail, you must allow "less secure" apps access in order for your SMTP settings to work. There are two places this setting must be enabled: The first is here: https://myaccount.google.com/ under “Connected apps & sites.”

How do I send an email using JavaMail?

Steps to send email using JavaMail APIGet the session object that stores all the information of host like host name, username, password etc. compose the message. send the message.


1 Answers

In Java you would do something similar to:

Transport transport = session.getTransport("smtps"); transport.connect (smtp_host, smtp_port, smtp_username, smtp_password); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();     

Note 'smtpS' protocol. Also socketFactory properties is no longer necessary in modern JVMs but you might need to set 'mail.smtps.auth' and 'mail.smtps.starttls.enable' to 'true' for Gmail. 'mail.smtps.debug' could be helpful too.

like image 133
maximdim Avatar answered Oct 14 '22 22:10

maximdim