我已经找到了相当多的参考资料来配置Glassfish的电子邮件,然而我还没有能够解决我自己的问题,我希望有人能帮助我。
我已经在Glassfish 3.12控制台中使用邮件主机、用户、发件人地址和描述配置了JavaMail会话。传输协议设置为SMTP,并且我添加了一个mail.smtp.host (和mail.smtp.auth=false)属性。
我用来发送邮件的代码如下:
public class JndiMail {
@Resource(name = "mail/[my-email]")
private Session mailSession;
public void sendMessage() {
Message msg = new MimeMessage(mailSession);
try {
msg.setSubject("[app] Email Alert");
msg.setRecipient(RecipientType.TO,
new InternetAddress("user@domain",
"User name"));
msg.setText("Hello ");
Transport.send(msg);
}
catch(MessagingException me) {
System.out.println(me.toString());
}
catch(UnsupportedEncodingException uee) {
}
}
}每次应用程序发送电子邮件时,我都会收到一条日志消息,说明本地主机已拒绝该电子邮件。我正在尝试使用远程交换服务器,而不是本地主机。我不明白为什么远程电子邮件服务器没有被访问?我意识到这应该是相当直截了当的,所以如果我错过了什么,我道歉。
下面是Glassfish日志:
[#|2013-03-26T10:00:39.334+1100|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=29;_ThreadName=Thread-2;|DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]|#]
[#|2013-03-26T10:00:39.334+1100|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=29;_ThreadName=Thread-2;|DEBUG SMTP: useEhlo true, useAuth false|#]
[#|2013-03-26T10:00:39.334+1100|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=29;_ThreadName=Thread-2;|DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false|#]发布于 2013-03-26 13:54:45
我已经解决了这个问题。解决方案相当简单。在Glassfish中配置Javamail资源时,在JNDI名称上使用"mail/“前缀是很重要的。我以为这是Glassfish加的。我通过删除上述代码中的注入并在try catch中包含JNDI查找来找到解决方案:
try {
InitialContext ctx = new InitialContext();
Session session = (Session) ctx.lookup("mail/MyEmail");
Message msg = new MimeMessage(session);https://stackoverflow.com/questions/15609237
复制相似问题