Reputation: 23
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("[email protected]", "123456");
}
};
Session session = Session.getDefaultInstance(props, authenticator);
session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("[email protected]","dqlhgys"));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
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
Upvotes: 2
Views: 8874
Reputation: 29971
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.
Upvotes: 7