2012-07-25 44 views
0

我在使用Glashfish 3.1.1上的Java郵件發送電子郵件時遇到了問題。Glassfish上的Java郵件AS

服務器不會拋出任何異常,僅向收件人發送僅包含頭的空郵件。運行沒有Glashfish的Java郵件,一切正常。

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

創建會話後,在您的應用程序中調用session.setDebug(true)。然後查看服務器日誌文件以查看JavaMail調試輸出是否有任何關於錯誤的線索。我假設你沒有得到任何例外。

+0

謝謝Bill,我找到了!問題出在FreeMarker上,我使用它來爲html消息構建模板。通過調試,我發現模板的路徑是錯誤的,它創建了一條空的消息。 – 2012-07-26 14:02:02