While sent email showing error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp8.net4india.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
My email settings is:
String mailProcessed = "";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp8.net4india.com");
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(frmMail, frmPass);
}
});
remove props.put("mail.smtp.starttls.enable", "true");
and add props.put("mail.smtp.ssl.enable", "true");
You are using SSL, not TLS.
Also make sure the mail server supports SSL.
Also make sure that the mail server is using TCP protocol SMTP, they might be using IMAP or POP3.
In that case you need to change the properties you reference by the protocol they use, i.e:
props.put("mail.pop3.socketFactory.port", "25");
props.put("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");
You are using getDefaultInstance for the session, but if a session was already established it will return the existing session and your properties will be lost.
Check the common mistakes page, it has it all:
https://javaee.github.io/javamail/FAQ#commonmistakes
Related
I have written a simple java program to connect to TLS enabled SMTP server as below
props.put("mail.smtp.host",GMAIL SMTP SERVER );
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", 587);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
loSession = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("ak#gmail.com",
GMAIL ACCOUNT PASSWORD)
Later setting the from email address as
InternetAddress loFromIA = new InternetAddress( "ashu#gmail.com");
loMsg.setFrom( loFromIA );
Now once i executed the code then i am receiving email from address ak#gmail.com which is SMTP server account address instead of ashu#gmail.com. I have explicitly set the from address as ashu#gmail.com but Gmail is overriding it. Same is happening with Microsoft server also.
Can someone suggest what needs to be fixed in code ? Also this happens after adding startTLS in code. without starttls it was working fine.
This is the code:
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtps");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "mail.mydomain.com");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.user", "myemail#mydomain.com");
properties.put("mail.smtp.password", "mypassword");
Session sendSession = Session.getInstance(properties);
MimeMessage message = new MimeMessage(sendSession);
message.setFrom(new InternetAddress("myemail#mydomain.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
Transport transport = sendSession.getTransport("smtp");
transport.connect("mail.mydomain.com", 465, "mydomain.com#mydomain.com", "mypassword");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
These are the SSL/TLS setting:
Outgoing Server: mail.mydomain.com
SMTP Port: 465
Executing the above code, it get stuck at:
transport.connect("mail.mydomain.com", 465, "mydomain.com#mydomain.com", "mypassword");
and nothing happen.
Why it get stuck at connect and how to fix it?
Try changing the mail.transport.protocol value from smtps to smtp.
You are trying to use port 465 which is for smtp . So you have to change the protocol to smtp . You are trying to start TLs with secure mode but you are using simple smtp port.
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtps");
properties.put("mail.smtp.starttls.enable", "true");
.....
Try changing it and send mail.
I found the problem.
At Transport transport = sendSession.getTransport("smtp");
Instead of smtp should be smtps
Thanks to everybody who tried to help me.
I'm having some troubles sending emails via Javamail using my company exchange server. We have an application that was sending emails via the gmail server without any problems, but for some changes in the policies of Google we want to use the company server to do the job.
Im sure the problem in the session properties, but i cant find a way to make it work
Properties props = new Properties();
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.host", _server);
session = Session.getInstance(props, this);
try {
transport = session.getTransport("smtp");
transport.connect("mail.company.com",_user,_pass);
transport.close();
This is the error is showing the log
javax.mail.MessagingException: Could not connect to SMTP host: mail.company.com, port: 443;
nested exception is:
avax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
You have to check your email provider and its SMTP settings; server, port and encryption method.
The following code snippet works with me
Put
//1) get the session object
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
// You have missed this line.
properties.put("mail.smtp.starttls.enable", "true");
// This SMTP server works with me for all Microsoft email providers, like: -
// Outlook, Hotmail, Live, MSN, Office 365 and Exchange.
properties.put("mail.smtp.host", "smtp.live.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.user", user);
properties.put("mail.smtp.pwd", password);
Session session = Session.getInstance(properties, null);
session.setDebug(true); // To trace the code implementation.
Transport transport = session.getTransport("smtp");
transport.connect("smtp.live.com", 587, user, password);
transport.close();
instead of
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.host", _server);
session = Session.getInstance(props, this);
try {
transport = session.getTransport("smtp");
transport.connect("mail.company.com",_user,_pass);
transport.close();
I found this website so helpful, in getting other email providers SMTP settings information.
I have a program that allows users to send emails. To do that my code asks the user to enter their email (the sender email). Then the program checks to see what comes after the '#'. If its gmail then I set the properties as such:
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
If its yahoo, then I set the properties as such:
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port","465"); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.host","smtp.mail.yahoo.com");
props.put("mail.smtp.port","465");
Then I create a session object using the properties as such:
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
The above works fine. What I want to do is let the program know what the settings should be in case the user has a domain that is hosted on google or microsoft. So for example instead of the email being yyy#gmail.com, it would be yyy#customer-domain.com. Is there a way for my program to know where the email is hosted or will I have to let the program ask the user? Thanks.
I am creating a application in which i am sending mail to users.
But the problem is while i am using Transport.send(msg) method, it displays the following error:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.
I am using the following properties to send mail.
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", smtpHostName);
props.put("mail.user", smtpUser);
props.put("mail.password", smtpPass);
props.put("mail.smtp.auth", smtpAuth == 1 ? true : false);
props.put("mail.port", smtpPort);
props.put("mail.smtp.starttls.enable", smtpTLS == 1 ? true : false);
Please help me to resolve this problem.
Try this
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", port);
And while creating session use this authentication code
l_session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
l_session.setDebug(true);
Our environment is running under WebSphere 7. To support secured smtps, correct variant to turn on SMTPS AUTHorization is:
"mail.smtps.auth=true"
Actually, according to decompiled sources, before authorizing the mail session WebSphere checks if there is a property composed as: "mail."+ transportProtocol + ".auth" set to "true".
This helped me to send emails via gmail.