jakarta-mail - JavaMail work on localhost but not on server
问题描述
I've strange issue with JavaMail, with the following code, all work fine when I run it on localhost, but when I deploy it on server this code fail. The execution stop on:
MimeMessage message = new MimeMessage(session);
The strange thing is that the execution comes freeze on this line, no Exception where launched and code not return or break execution, everything freezes. Where am I wrong?
Here the full code:
package app.util.mail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.logging.Logger;
public class MailSender {
//Destinatari [un indirizzo per ciascuna delle 3 tipologie]
private static final String INFO_ADDR = "xxx@domain.eu";
private static final String ADMIN_ADDR = "xxx@domain.eu";
private static final String ISTITUZ_ADDR = "xxx@domain.eu";
//Mittente
private static final String FROM = "postmaster@domain.eu";
//PWD di autenticazione mittente
private static final String PWD = _PWD_;
//Provider SMTP in Uscita
private static final String SMTP_PROVIDER = "smtps.aruba.it";
private static final String SMTP_PORT = "465";
public static void send(String type, String subject, String text) throws Exception{
//Logger log = Logger.getLogger(MailSender.class.getName());
// Get system properties
Properties properties = System.getProperties();
// Setup mail server (smtp protocol)
properties.put("mail.smtp.host", SMTP_PROVIDER);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.port", SMTP_PORT);
properties.put("mail.smtp.ssl.trust", "*");
// Get the default Session object.
Session session = Session.getInstance(properties, new Auth(FROM, PWD) );
//Destinatario in base al tipo di richiesta
String to = "";
switch( type ) {
case "info":{ to = INFO_ADDR; break;}
case "admin":{ to = ADMIN_ADDR; break;}
case "istituz":{ to = ISTITUZ_ADDR; break;}
default:{ /* lancia errore */ break;}
}
//Encapsulation and than send Message
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
//Mittente
message.setFrom(new InternetAddress(FROM));
//Destinatario
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
//Oggetto del Messaggio
message.setSubject(" XXX ");
//Corpo del Messaggio
message.setText(text);
Transport.send(message);
}
}
/**
* Inner-Class for Client Authentication
*/
class Auth extends Authenticator {
private static String FROM, PWD;
protected Auth(String from, String pwd) {
FROM = from;
PWD = pwd;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM, PWD);
}
}
解决方案
推荐阅读
- php - 动态传递产品 ID 作为重力表单上的主要产品字段(产品附加)
- r - 从另一个数据框中删除具有真实值的行
- r - 从 2 列 1
- r - R中有没有办法将列中的字符串拆分为多列并为每个子字符串添加行?
- c# - 如何制作 Xunit 测试类并在 Nunit 中使用带有参数的构造函数参数(如 TestFixture)?
- r - 如何在数据集中使用多个子集?
- c# - 如何从数据库中获取 ViewModel 中的数据并将其显示在 Asp.net MVC 中查看?
- python - 做什么
和 意思是? - scala - 如何在 Apache Spark 中执行数据集的加权分区
- css - 如何设置 css 以使选择按钮在向下滚动时不会移动?