Error sending mail - java

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);

Related

How to send ZOHO SMTP server mail in 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.

Overriding URLStreamHandler causes Unrecognized SSL message, plaintext connection Error [duplicate]

I have a java complied package to speak with the https server on net. Running the compilation gives the following exception:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
I think this is due to the connection established with the client machine is not secure. Is there any way to configure the local machine or ports in order to connect to the remote https server?
I think this is due to the connection
established with the client machine is
not secure.
It is due to the fact that you are talking to an HTTP server, not an HTTPS server. Probably you didn't use the correct port number for HTTPS.
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
You should have a local SMTP domain name that will contact the mail server and establishes a new connection as well you should change the SSL property in your programming below
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection
props.put("mail.smtp.socketFactory.fallback", "true"); // Should be true
I got the same error message when I forgot to log in to the company firewall, before performing a POST request through a proxy.
I got the same error. it was because I was accessing the https port using http.. The issue solved when I changed http to https.
Adding this as an answer as it might help someone later.
I had to force jvm to use the IPv4 stack to resolve the error. My application used to work within company network, but while connecting from home it gave the same exception. No proxy involved. Added the jvm argument
-Djava.net.preferIPv4Stack=true and all the https requests were behaving normally.
If you are running local using spring i'd suggest use:
#Bean
public AmazonDynamoDB amazonDynamoDB() throws IOException {
return AmazonDynamoDBClientBuilder.standard()
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("fake", "credencial")
)
)
.withClientConfiguration(new ClientConfigurationFactory().getConfig().withProtocol(Protocol.HTTP))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("localhost:8443", "central"))
.build();
}
It works for me using unit test.
Hope it's help!
I face the same issue from Java application built in Jdevelopr 11.1.1.7 IDE. I solved the issue by unchecking the use of proxy form Project properties.
You can find it in the following:
Project Properties -> (from left panle )Run/Debug/Profile ->Click (edit) form the right panel -> Tool Setting from the left panel -> uncheck (Use Proxy) option.
i solved my problem using port 25 and Following prop
mailSender.javaMailProperties.putAll([
"mail.smtp.auth": "true",
"mail.smtp.starttls.enable": "false",
"mail.smtp.ssl.enable": "false",
"mail.smtp.socketFactory.fallback": "true",
]);
In case you use Jetty version 9 or earlier you need to add it to jetty
by
RUN java -jar ${JETTY_HOME}/start.jar --add-to-startd=https
and according to this
Jetty: How to use SSL in Jetty client side
from Jetty version 10 it should work out of the box
In case you are running
Cisco AnyConnect Secure Mobility Agent
Cisco AnyConnect Web Security Agent
try stopping the service(s).
In our corporate network this IS the solution to the issue.
It worked for me now, I have change the setting of my google account as below:
System.out.println("Start");
final String username = "myemail#gmail.com";
final String password = "************";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Transport transport=session.getTransport();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemail#gmail.com"));//formBean.getString("fromEmail")
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("myemail#gmail.com"));
message.setSubject("subject");//formBean.getString(
message.setText("mailBody");
transport.connect();
transport.send(message, InternetAddress.parse("myemail#gmail.com"));//(message);
System.out.println("Done");
} catch (MessagingException e) {
System.out.println("e="+e);
e.printStackTrace();
throw new RuntimeException(e);
}
Though I have enabled SSL and TSL while running program in this link of same post. I spend a lot of time but than I realized and found this link.
And done 2 following steps and setting control in google. :
Disable the 2-step verification (password and OTP)
Enabling to allow to access less secure app(Allow less secure apps:
ON.)
Now I am able to send mail using above program.
As EJP said, it's a message shown because of a call to a non-https protocol.
If you are sure it is HTTPS, check your bypass proxy settings, and in case add your webservice host url to the bypass proxy list
if connection is FTPS test:
FTPSClient ftpClient = new FTPSClient(protocol, false);
protocol = TLS,SSL
and false = isImplicit.
I got the same issue and it got resolved by setting "proxyUser" and "proxyPassword" in system properties.
System.setProperty("http.proxyUser", PROXY_USER);
System.setProperty("http.proxyPassword", PROXY_PASSWORD);
along with "proxyHost" and "proxyPort"
System.setProperty("http.proxyHost", PROXY_ADDRESS);
System.setProperty("http.proxyPort", PROXY_PORT);
Hope it will work.
I was facing this exception when using Gmail.
In order to use Gmail I had to turn ON "Allow less secure apps".
This Gmail setting can be found at https://www.google.com/settings/security/lesssecureapps after login the gmail account.
I've got similar error using camel-mail component to send e-mails by gmail smtp.
The solution was changing from TLS port (587) to SSL port (465) as below:
<route id="sendMail">
<from uri="jason:toEmail"/>
<convertBodyTo type="java.lang.String"/>
<setHeader headerName="Subject"><constant>Something</constant></setHeader>
<to uri="smtps://smtp.gmail.com:465?username=myemail#gmail.com&password=mypw&to=someemail#gmail.com&debugMode=true&mail.smtp.starttls.enable=true"/>
</route>
Maybe your default cerficate has expired. to renew it through admin console go "Security >SSL certificate and key management > Key stores and certificates > NodeDefaultKeyStore > Personal certificates" select the "default" alias and click on "renew" after then restart WAS.
If you're running the Java process from the command line on Java 6 or earlier, adding this switch solved the issue above for me:
-Dhttps.protocols="TLSv1"

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

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'.

Categories