Добрый день. Я пытаюсь переписать наш код для отправки почты с помощью аутентификации ssl. Потому что наши системные администраторы добавили в постфиксный агент авторизации. В хостах на машине linux есть имя сервера для smtp сервера и ip. Я проверил соединение с telnet.
Итак, вот мой код, который создает свойства и сеанс.
private Properties createProperties() {
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", OurSTMPHostServer);
properties.setProperty("mail.smtp.port", "587"); //SSL Port
properties.setProperty("mail.smtp.socketFactory.port", "465"); //SSL Port
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.user", MAIL_USER);
properties.setProperty("mail.smtp.password", MAIL_PASSWORD);
return properties;
}
private Session createSession(Properties properties) {
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication
getPasswordAuthentication() { return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD); }
});
session.setDebug(true);
return session;
}
Здесь код создания сообщения
public MimeMessage createMimeMessage(String mailFromAddress, String mailFromName,
String subject, String mailTo, String message, String messageText, List<File> fileList) throws MessagingException, UnsupportedEncodingException {
MimeMessage msg = new MimeMessage(createSession(createProperties()));
msg.setFrom(new InternetAddress(mailFromAddress, mailFromName));
msg.setSubject(subject);
msg.setRecipients(Message.RecipientType.TO, mailTo);
MimeBodyPart wrap = new MimeBodyPart();
MimeMultipart cover = new MimeMultipart("alternative");
MimeBodyPart text = new MimeBodyPart();
text.setHeader("Content-Transfer-Encoding", "base64");
text.setContent(messageText, "text/plain;charset=UTF-8");
cover.addBodyPart(text);
MimeBodyPart html = new MimeBodyPart();
html.setHeader("Content-Transfer-Encoding", "base64");
html.setContent(message, "text/html;charset=UTF-8");
cover.addBodyPart(html);
wrap.setContent(cover);
MimeMultipart content = new MimeMultipart("related");
msg.setContent(content);
content.addBodyPart(wrap);
msg.saveChanges();
return msg;
}
Здеськод для отправки сообщения
public void sendTextMessage(String mailFromAddress, @Nullable String mailFromName, String subject, String mailTo, String message) {
MimeMessage msg = null;
try {
String messageText = convertService.htmlToText(message);
msg = createMimeMessage(mailFromAddress, mailFromName, subject, mailTo, message, messageText, Collections.<File>emptyList());
mailSenderFacade.send(msg);
}
}
Где mailSenderFacade - это класс JavaMailSender, где поле host = OurSMTPHostServer, порт = 465 UPD: здесь ошибка
<code><pre>org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketTimeoutException: Read timed out; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketTimeoutException: Read timed out
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)
at com.zina.service.mail.MailSenderFacade.send(MailSenderFacade.java:122)
at com.zina.service.mail.MailService.sendTextMessage(MailService.java:131)
at com.zina.service.mail.MailService.sendTextMessage(MailService.java:193)
at com.zina.service.person.PersonService.sendRegistrationApproveMail(PersonService.java:255)
at com.zina.service.person.PersonService.registerUser_aroundBody4(PersonService.java:167)
at com.zina.service.person.PersonService$AjcClosure5.run(PersonService.java:1)