Question

I'm facing problems to send e-mails with Java Mail on Glashfish 3.1.1.

The server doesn't throw any Exception, just send an empty message with only header to recipient. Running Java Mail without the Glashfish, everything works fine.

   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();
}

I think everything's fine with the code, I just don't understand, why it doesn't work on Glassfish?

I'd be grateful if someone help me. Thanks in advance.

Was it helpful?

Solution

Call session.setDebug(true) in your application after creating the Session. Then look at the server log file to see if the JavaMail debug output has any clues about what's going wrong. I'm assuming you're not getting any exceptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top