[英]Unable to send mail: Could not convert socket to TLS;
The following code is working as Java application eclipse, But when it deployed in SAP PO server the below exception is thrown: 以下代码正在作为Java应用程序的日食运行,但是当将其部署在SAP PO服务器中时,将引发以下异常:
Note : Stand alone java file is running on my laptop(Windows) where as server is not Linux 注意:独立的java文件正在笔记本电脑(Windows)上运行,而服务器不是Linux
javax.mail.MessagingException: Could not convert socket to TLS; javax.mail.MessagingException:无法将套接字转换为TLS。 nested exception is: java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl) 嵌套的异常是:java.net.SocketException:java.security.NoSuchAlgorithmException:构造实现时出错(算法:Default,提供者:SunJSSE,类:com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl)
public static void main(String[] args) {
PropertiesSerDto dto = new PropertiesSerDto();
dto.setEmailAuth("true");
dto.setEmailStarttlsEnable("true");
dto.setEmailHost("outlook.office365.com");
dto.setEmailPort("587");
dto.setEmailPassword("1234");
dto.setEmailFrom("test@mail.com");
dto.setEmailUser("test@mail.com");
String to="to@gmail.com";
String subject="Mail Testing";
String message = "Hello";
try {
sendHtmlEmail(dto,to,subject,message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void sendHtmlEmail(PropertiesSerDto dto, String to, String subject, String message) throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", dto.getEmailHost());
properties.put("mail.smtp.port", dto.getEmailPort());
properties.put("mail.smtp.auth", dto.getEmailAuth());
properties.put("mail.smtp.starttls.enable", dto.getEmailStarttlsEnable());
final String user = dto.getEmailUser();
final String password = dto.getEmailPassword();
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message messageContent = new MimeMessage(session);
messageContent.setFrom(new InternetAddress(dto.getEmailFrom()));
InternetAddress[] iAdressArray = InternetAddress.parse(to);
messageContent.setRecipients(Message.RecipientType.TO, iAdressArray);
messageContent.setSubject(subject);
messageContent.setSentDate(new Date());
// set plain text message
messageContent.setContent(message, "text/html");
// sends the e-mail
Transport.send(messageContent);
}
For the record to be left, I was having the same problem and thought it was an issue in the server configuration. 为了保留记录,我遇到了同样的问题,并认为这是服务器配置中的问题。 It turned out to be a firewall configuration. 原来是防火墙配置。 This was not letting out the connection requests to the smtp server. 这并未释放到smtp服务器的连接请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.