How to send ZOHO SMTP server mail in java? - java

I have been using ZOHO SMTP server for sending out email. but it was working in localhost server ,when deploying my code into AWS server it not going to be working,it is giving Exception like below..
javax.mail.MessagingException: Could not connect to SMTP host: smtp.zoho.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
My java code is :-
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.zoho.com");
prop.put("mail.smtp.port", 465);
prop.setProperty("mail.smtp.user", username);
prop.setProperty("mail.smtp.password", password);
prop.setProperty("mail.smtp.auth", "true");
// prop.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.setProperty("mail.smtp.socketFactory.fallback", "false");
prop.setProperty("mail.smtp.socketFactory.port", "465");
// prop.put("mail.smtp.starttls.enable", "true");
// prop.setProperty("mail.smtps.tls.enable", "true");
The above code is working in my localhost server (Tomcat).
can any one please help to solve out this.

465 is SSL port. It looks like SSL certificate from ZOHO is missing in AWS. Download/obtain certificate from Zoho and import it on AWS's Keystore. It should solve the problem.
Check https://help.zoho.com/portal/community/topic/ssl-security-certificate-updated-pop-imap-and-smtp-servers from similar problem and possible solutions.

Related

Java Mail Exception. Could not connect to SMTP host: localhost, port: 25;

I am trying to send an email from my java application, and I am attempting to send the email by using SSL. I keep getting the error
Could not connect to SMTP host: localhost, port: 25;
when I attempt to use the following code.
Properties props = new Properties();
props.put("mail.smtps.auth", "false");
props.put("mail.smtps.host", host);
props.put("mail.smtps.socketFactory.port", "465");
props.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtps.starttls.enable", "true");
props.put("mail.transport.protocol", "smtps");
Session session = Session.getInstance(props);
But if I use the following code it works just fine:
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.host", host);
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
The variable host is an ip address, so I think it's strange that the error states that I cannot connect to localhost on port 25, when I am not connecting to localhost and the port is not 25.
I need to be able to send the emails using SMTPS SSL, and not SMTP.
Is there something wrong in my code?
As all your properties are named mail.smtps.*, make sure to get your Transport instance as next:
Transport transport = session.getTransport("smtps");
While trying to debug, consider also switching to debug mode thanks to session.setDebug(true) in order to get more verbose log.
You forgot to set the port of smtp:
props.put("mail.smtp.port", String.valueOf(port));
Check properties info here:
JavaMail summary

Android SMTP mail sender with SSL ignore

Iam trying to use the JavaMail examples found online to send emails from my Android application using a custom SMTP server. The SMTP server does not have a valid SSL certificate, instead it uses a self signed one so when I try to connect I get an SSL Handshake exception.
I tried to use the :
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.checkserveridentity", "false");
props.put("mail.smtp.ssl.trust", "*");
properties but then I get the error:
E/SendMail(17874): Could not connect to SMTP host: {MY_SMTP_HOST}, port: {MY_SMTP_PORT}, response: -1
Any suggestions? Is there any other library that supports sending emails through SMTP?

DSpace: Java mail doesn't work in the company's network

I set up Dspace to send e-mail using my company's e-mail server, DSpace uses javax.mail to do this.
When I try send outside of my company's network the e-mail is sent successfully, but in the company's network the email is not sent.
Follows the shown error message:
Error: javax.mail.MessagingException: Exception reading response;
nested exception is: javax.net.ssl.SSLException: Unrecognized SSL
message, plaintext connection?
(1) There's no proxy on the way
(2) I tryed to send using gmail, but I've got same scenario
(3) The only related thread that I've found were solved with proxy settings, but because (1) that is not a solution (Java mail doesn't work in the company's network)
(4) The used door (587) is the same configured in Thunderbird which works great both inside and outside my company's network.
WHAT A MISTERY!!! O.O
If some one could help, that would be great!
Thanks a lot!
I found an answer in another thread that guided me:
Javamail non ssl properties are:
Properties props = new Properties();
props.put("mail.smtp.user", "email#mydomain.com");
props.put("mail.smtp.password", "password");
props.put("mail.smtp.protocol", "smtp");
props.put("mail.smtp.host", "smtp.mydomain.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable","false");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.smtp.socketFactory.class", "");
props.put("mail.smtp.socketFactory.fallback", "false");
The only property that I changed to DSpace start send email was mail.smtp.socketFactory.class="", before was "javax.net.ssl.SSLSocketFactory".
Unfortunately I still not understand why previous settings were working outside company's network, maybe some rule blocking ssl connections.
#SergeBallesta: Thanks for the help!

is starttls.enabled = true is safe for mail sending from java code?

I'm sending emails from java code.My configuration is
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "****");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
On my other question EJP answered
Any protocol that uses STARTTLS is in SSL mode after the STARTTLS
command is issued
But my debug output shows:DEBUG SMTP: trying to connect to host "****", port 587, isSSL false.
So my question sounds like
Is such configuration really safe and uses SSL as EJP said despite of isSSL=false on my debug output?
UPDATE
connecting code
Transport transport = session.getTransport("smtp");
transport.connect("host", 587,"username", "password");
when I wrote Transport transport = session.getTransport("smtps") I got
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection
Set mail.smtp.starttls.required=true
This ensures TLS is used or connection won't happen
REF: https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html
You should still use the 'smtp' transport as that is the protocol (smtps is not an known protocol). SSL is used for the connection.
I myself have used 'javax.mail' in conjunction with Google. Google only allows SSL. So I can only answer yes; it works and it is safe.
I should add that the property 'mail.smtp.starttls.enable' is a bit confusing. The startsll property refers to the command that is being issued but it should have been named: 'mail.smtp.secure.enable'.

Error sending mail

I've got the following...
Properties prop = new Properties();
prop.put(host, "localhost");
prop.put(host, "25");
When I run the following it throws me back the following error...
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused
I don't really understand why it's throwing me this back... anyone have any ideas? my smtp server is in fact up and working as far as I can tell :S
edit: If I understand correctly it's telneting to my machine.. why? it shouldn't be... It's meant to be telneting to host.
I think you have the property to set the host incorrect:
prop.put(host, "localhost");
This will create a property with the key of whatever is in host and call it localhost. I'm guessing that your property isn't what you think it is so JavaMail is then using the default of localhost.
You propably want this instead:
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);

Categories