使用Exchange进行Glassfish电子邮件配置

时间:2013-03-25 06:51:32

标签: java-ee glassfish-3

我找到了很多关于为电子邮件配置Glassfish的参考资料,但是我无法解决自己的问题,我希望有人可以提供帮助。

我在Glassfish 3.12控制台中使用mailhost,用户,发件人地址和描述配置了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) {
        }
      }
}

每次应用程序发送电子邮件时,我都会收到一条日志消息,指出本地主机已拒绝该电子邮件。我正在尝试使用远程交换服务器 - 而不是localhost。我不明白为什么没有访问远程电子邮件服务器?我意识到这应该是相当直接的,所以如果我错过了什么,我会道歉。

这些是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|#]

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。解决方案相当直接。在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);