我在Glashfish 3.1.1上发送带有Java Mail的电子邮件时遇到了问题。
服务器不会抛出任何异常,只需向收件人发送只有标题的空消息。 没有Glashfish运行Java Mail,一切正常。
public void sendHtmlMessage(String subject, String html){
// Creates a email Session and
// and Authenticator attached to it
Session session = getMailSession();
try{
MimeMessage message = createMessage(session, subject, MailSender.MIME_Type.TEXT_HTML, html); // creates message
transportSMTPMessage(session, message); // transport message
} catch(AddressException e){ log("Internet address is invalid!"); e.printStackTrace(); }
catch(NoSuchProviderException e){ log("Host is invalid!"); e.printStackTrace(); }
catch(MessagingException e){ log("Message is invalid!"); e.printStackTrace(); }
catch(Exception e){ log("Gereric Exception!"); e.printStackTrace(); }
}
// Helper to obtain Mail Session
private Session getMailSession(){
Properties props = new Properties();
return Session.getInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
}
// Helper to create MimeMessage
private MimeMessage createMessage(Session session, String subject, MailSender.MIME_Type mime, String content)
throws AddressException, MessagingException{
// Some code to create the message...
message.saveChanges();
return message;
}
// Helper to transpot the message thru the SMTP protocol
private void transportSMTPMessage(Session session, Message message)
throws NoSuchProviderException, MessagingException{
// Creates the transport connection and
// Send the message
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
我觉得代码一切都很好,我只是不明白,为什么它不适用于Glassfish?
如果有人帮助我,我将不胜感激。 提前谢谢。
答案 0 :(得分:0)
创建Session后,在应用程序中调用session.setDebug(true)。然后查看服务器日志文件以查看JavaMail调试输出是否有任何关于出错的线索。我假设你没有任何例外。