users@glassfish.java.net

Re: Setup of Glassfish Email to use remote server and Port

From: <glassfish_at_javadesktop.org>
Date: Tue, 01 Jul 2008 16:42:43 PDT

Hi there, I don't know if you ever got this working but I finally was able to dig into it. From looking at issue 4498, it seems there probably were some properties missing that you needed. I'm going guess that you need to add these to your mail resource

[code]
    <property name="mail-smtps-starttls-enable" value="true"/>
    <property name="mail-smtps-socketFactory-port" value="465"/>
    <property name="mail-smtps-socketFactory-class" value="javax.net.ssl.SSLSocketFactory"/>
    <property name="mail-smtps-socketFactory-fallback" value="false"/>
[/code]

(or use "mail-smtp-..." prefix if you specified that protocol). I'm a bit unclear on the port though - you specified 993 in issue 4498 - that's the IMAP SSL port. 465 is the SMTP SSL port, but maybe Exchange is a little weird... I believe the socket factory port should be the same value you specified for "mail-smtps-port"

The following mail resource + code block works to send email via GMail SMTP SSL (if you put in valid username/password of course). Note - the password field does not have to be in the resource, but could come from the servlet session, etc. If you do store a password in the resource, note this is stored as plain text in domain.xml, so you probably want to secure it via the password alias mechanism. See http://wiki.glassfish.java.net/attach/GlassFishAdministrationPages/aliased-passwords.html

[code]

    <mail-resource debug="true" enabled="true" from="${youruserid}_at_gmail.com"
            host="smtp.gmail.com" jndi-name="mail/gmailSession" object-type="user"
            store-protocol="imaps" store-protocol-class="com.sun.mail.imap.IMAPStore"
            transport-protocol="smtps" transport-protocol-class="com.sun.mail.smtp.SMTPTransport"
            user="${youruserid}">
        <description>GMail Mail Resource</description>
        <property name="mail-smtps-host" value="smtp.gmail.com"/>
        <property name="mail-smtps-port" value="465"/>
        <property name="mail-smtps-auth" value="true"/>
        <property name="mail-smtps-user" value="${youruserid}"/>
        <property name="mail-smtps-password" value="${yourpassword}"/>
        <property name="mail-smtps-starttls-enable" value="true"/>
        <property name="mail-smtps-socketFactory-port" value="465"/>
        <property name="mail-smtps-socketFactory-class" value="javax.net.ssl.SSLSocketFactory"/>
        <property name="mail-smtps-socketFactory-fallback" value="false"/>
    </mail-resource>

// Servlet code...

    @Resource(name = "mail/gmailSession")
    private Session mailSession;

    /** email = recipient_at_their_server
     * subject, body = message text
     */
    void sendMail(String email, String subject, String body) throws MessagingException, NamingException {
        // username and password could come from anywhere, e.g. servlet session, etc.
        Properties props = mailSession.getProperties();
        String username = (String) props.get("mail.smtps.user");
        String password = (String) props.get("mail.smtps.password");

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject(subject);
        message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(email, false));
        message.setText(body);
        message.saveChanges();

        Transport transport = mailSession.getTransport("smtps");
        try {
            transport.connect(username, password);
            transport.sendMessage(message, message.getAllRecipients());
        } finally {
            transport.close();
        }
    }
[/code]
[Message sent by forum member 'peterwx' (peterwx)]

http://forums.java.net/jive/thread.jspa?messageID=283824