I am not able to connect to smtp port no 465, which uses SSL authentication. Earlier it used to work in default port 25, but have changed the setting to point to 465 for outbound emails for all outgoing emails. This works perfectly fine when i use outlook but shows that it cannot connect to port 465.
Below is the error.
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.bizmail.yahoo.com, port: 465
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
And below is the Javax Mail setting that i use.
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.socketFactory.class",
props.setProperty("mail.smtp.port", "465");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.server.username", "test#gamil.com");
props.put("mail.server.password", "test123");
props.put("mail.smtp.auth", "true");
props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
Session session = Session.getInstance(props, authenticator);
session.setDebug(sessionDebug);
Any help would be much appreciated.
As the log says, smtp.bizmail.yahoo.com at port 465 cannot be connected, if you are using gmail, the host should be:smtp.gmail.com.
For reference:
Gmail- Host: smtp.gmail.com , Port: 465
Hotmail- Host: smtp.live.com , Port: 587
Yahoo- Host: smtp.mail.yahoo.com , Port: 465
Related
I'm trying to use javamail to send out an email. It worked in a java application. But i got some errors when i use it in my Weblogic web application.
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTPS");
props.setProperty("mail.smtp.host", "msg.petrochina.com.cn");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.auth", "true");
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.socketFactory", sf);
final Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("dqlhgysbj#petrochina.com.cn", "123456");
}
};
Session session = Session.getDefaultInstance(props, authenticator);
session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("dqlhgysbj#petrochina.com.cn","dqlhgys"));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("dqlhgysaccept#petrochina.com"));
mimeMessage.setSubject("test");
mimeMessage.setSentDate(new Date());
mimeMessage.setText("testMailContent","utf-8");
mimeMessage.saveChanges();
Transport.send(mimeMessage);
When i run the app I can see the following in my log
DEBUG: set
DEBUG: JavaMail version 1.4.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "msg.petrochina.com.cn", port 465, isSSL false
220 petrochina.com.cn [20137] ESMTP MTA v8.1.2; Mon, 03 Dec 2018 15:59:04 +0800
DEBUG SMTP: connected to host "msg.petrochina.com.cn", port: 465
EHLO SUNWAY-DEV3
250-petrochina.com.cn
250-SIZE 104857600
250-AUTH=LOGIN PLAIN
250-AUTH LOGIN PLAIN
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "SIZE", arg "104857600"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
STARTTLS
454 tls initialize failed: ssl accept error. (#4.7.0) (eYou MTA)
javax.mail.MessagingException: 454 tls initialize failed: ssl accept error. (#4.7.0) (eYou MTA)
It seems the application is trying to connect without ssl, and it won't connect.But i can connect the address with telnet.telnet
First, fix all these common JavaMail mistakes.
Then, remove the mail.transport.protocol setting. ("SMTPS" is the wrong protocol name, it should be "smtps", but I believe it's being ignored because of the above problems, and you don't want it because of the properties you're setting.)
Depending on your mail server, you should need either mail.smtp.ssl.enable to use SSL when first connecting to the server, or you should need mail.smtp.starttls.enable to connect usingplain text and then switch to SSL/TLS after connecting. You should never need both.
Hopefully that will fix your problem.
But note that you're using a very old version of JavaMail, which must mean you're using a very old version of WebLogic. If possible, upgrade to a newer version of WebLogic.
I have created Session successfully.
I am able to get IMAP urlName but when trying to make connection of Store for outlook, I get an error.
What hostname will come in parameter for outlook?
You could use the below code to connect outlook:
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// props.put("mail.smtp.host", "smtp.gmail.com");smtp.office365.com
props.put("mail.smtp.host", "smtp.office365.com");//This is hostname
props.put("mail.smtp.port", "587"); //change this port
Before connection, You should check few things:
SMTP server should be appropriate.
Some organisations has blocked 587 port number, please check with your IT team if
that port is open to communicate with given SMTP server.
For more information, you could refer to this link:
java outlook send mail
The email is working properly in windows server 2008, but when I changed to windows server 2012, its causing this error.
SystemException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.sendgrid.net, port: 25; nested exception is: java.net.SocketException: Permission denied: connect
I have googled about this and they told to -Djava.net.preferIPv4Stack=true, put this entry in jvm system property and also asked to disabled the antivirus. Both didn't worked. Also I tried contacting the ISP to open the 25 port and now the 25 port is listening. This is my code.
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", 25);
props.put("mail.smtp.auth", "true");
SMTPAuthenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
//read the Template and replace values using Velocity Engine
String text = messageContent;
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
I am using sendgrid, java8, struts 2 and apache tomcat 8.You are my last hope, Please help me with that. Thanks in advance.
I am having similar type of error so i do like this, and now the code is working fine for me Plus i also disable all the antivirus software from my system they generally cause this problem.
Hope it works for you.
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); // SMTP Host
props.put("mail.smtp.port", "587"); // TLS Port 25/587
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true"); // enable authentication
I set following property to send a mail to gmail without authenticating but i am not able to send. please help me
Properties props = new Properties();
props.put("mail.smtp.host", SMTPServer);
//props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTPPort);
1) port 465 is ssl port, therefore you need create SSL connection (you comment this string)
//props.put("mail.smtp.ssl.enable", "true");
2) I'm sure that smtp.gmail.com doesn't allow connection without authentification, because everybody could send mail from any user....
edit:
And check telnet to smtp.gmail.com 465
look at the article for sending mail
http://www.journaldev.com/2532/java-program-to-send-email-using-smtp-gmail-tls-ssl-attachment-image-example
I'm trying to send mails using javax.mail from an hotmail account.
Until now I've tested my code also using gmail account and everything works fine, but
with hotmail nothing works in particular, I receive a MailConnectException.
This is the code that I use in order to send the mail:
props.put("mail.starttls.enable", true);
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", true);
props.put("mail.smtp.auth", true);
As you can see I've created a PropertiesFactory in order to create a specific instance of the Properties object for each specific mail host that I use. In the code there is the factory for the HotmailProperties structure.
Starting a debugging session I've checked that useAuth option and isSSL are both equal to false.
What do I change in the properties configuration? Maybe there are some other errors in the code?
This is the debugging result of my program:
http://ideone.com/SDu4JG
SOLVED
I've solved my problem with the hotmail server.
Looking to this page: http://windows.microsoft.com/en-us/windows/outlook/send-receive-from-app
I've understood that hotmail, differently from the other mail servers like gmail and yahoo, considers the username as the complete email address. So when I do the login I always receive an error.
Finally it works. Thank you to all.
From your debug output:
MAIL FROM:<shadowtemplate#hotmail.com>
530 5.7.0 Must issue a STARTTLS command first
DEBUG SMTP: got response code 530, with response: 530 5.7.0 Must issue a STARTTLS command first
Status code 530 means that the client was not authenticated. (I googled "SMTP error status code 530" to get this)
Your message above indicates that TLS (transport layer security: encryption) is required
Apparently, Hotmail requires that you send mail over smtps and the default port is 587.
It's Working for me . Surely it will work for u
:--
with port 587:--
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.setProperty("mail.smtp.quitwait", "false");
props.put("mail.smtp.starttls.enable", "true");
Session smtpSession = Session.getInstance(props1, authenticator);
smtpSession.setDebug(true);
You named the properties mail.smtps instead of mail.smtp
There is already a question which is solved, have a look: Java Mail over TLS