Send email get stuck at transport.connect - java

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.

Related

Sending Email using Javamail via Exchange

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.

Could not connect to SMTP host: smtp8.net4india.com, port: 25;

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

Reading Mails from Outlook Javamail

Am trying to read the emails from the outlook using javamail
herez the code snippet.
try {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imaps.starttls.enable", "true");
props.setProperty("mail.imaps.host", "outlook.office365.com");
props.setProperty("mail.imaps.port", "143");
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
Store mailStore = mailSession.getStore("imaps");
mailStore.connect("outlook.office365.com", "<username>", "<password>");
} catch (Exception ex){
ex.printStackTrace();
}
Exception
javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:727)
Any pointers are appreciated.
Your code is very confused. Keep it simple:
Properties props = new Properties();
props.setProperty("mail.imap.ssl.enable", "true");
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
Store mailStore = mailSession.getStore("imap");
mailStore.connect("outlook.office365.com", "<username>", "<password>");
Use the "imap" protocol, but tell JavaMail to enable SSL. Don't worry about ports, JavaMail knows what to do. More detail in the JavaMail FAQ.
Port 143 (what you're using) is for plain-text IMAP (hence the error message plaintext connection?). IMAPS uses port 993, so try that.

Java Mail Change port in Transport without Authentication username and password

Instead of port 25 I want to be able to set the port to be whatever I want, along with the host. But I do not want to set the username or password because credentials will vary. My code base was working fine just using the static method Transport.send() to perform the task of sending out emails.
Transport has a connect method with no arguments or a method host/port/auth credentials.
There does not seem to be a way to set just the port and host without using the other two.
Transport seems to default to 25 no matter what I try. I am using a tool called smtp4dev to listen on ports that I am testing.
The important bits of the code are below:
Properties props = new java.util.Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "localhost");//Just using localhost for testing
props.put("mail.smtp.port", 3500);//Whatever port
props.setProperty("mail.smtp.auth", "false");
session = Session.getInstance(props);
msg = new MimeMessage(session);
Transport transport = session.getTransport("smtp");
transport.connect(); //transport.connect("localhost", 3500, "","");
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
transport.close();
I can inspect session and see that it has the port property set in it, but when I inspect transport I only see the defaultPort as being set. I tried using a blank username, and password to no avail. Any ideas? Am I doing something dumb?
This is what fixed it, just use SMTPTransport instead and it works. I also used port as a string instead of an int.(I then had to unblock the port)
import com.sun.mail.smtp.SMTPTransport;
Properties props = new java.util.Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "localhost");//Just using localhost for testing
props.setProperty(SMTP_PORT_PROPERTY, String.valueOf(3500));//Whatever port
props.setProperty("mail.smtp.auth", "false");
session = Session.getInstance(props);
msg = new MimeMessage(session);
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect(); //transport.connect("localhost", 3500, "","");
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
transport.close();
The data type of the property value "mail.smtp.port" must be string.
Try changing this:
props.put ("mail.smtp.port", 3500);
to:
props.put ("mail.smtp.port", "3500");

Return path doesn't get set using mail.smtp.from

I am having problems setting the return path for all yahoo emails I'm sending. Here's what I'm using:
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.auth", "true");
props.put("mail.host", host);
props.put("mail.port", port);
props.put("mail.user", username);
props.put("mail.password", password);
props.put("mail.smtp.from", bounceAddress);
I'm sending the mimeMessage using transport.sendMessage method. Still, after sending a few, in the Full-header for yahoo mails the return path is the same as the sender email.
Any idea what might be the problem?
Thanks
You're using the "smtps" protocol, but setting the property for the "smtp" protocol. Set "mail.smtps.from" instead.

Categories