commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From myllokunmingia <myllokunmin...@gmail.com>
Subject Commons Email 1.1 with Gmail and Provided Mail Session
Date Wed, 08 Jul 2009 12:48:35 GMT

Hi we're attempting to use a mail session provided by an application server
with commons email 1.1 and the code below is the only way we could get
commons email to work with the gmail smtp server when providing a session.
If a session is not provided commons email is able to send an email using
the gmail smtp server. Are we approaching the problem correctly? Or is there
a way to incorporate the changes below into the code base?

Thanks in advance,
Dan

            // set through server mail session cofiguration           
            // Properties props = new Properties();           
            //props.put("mail.smtps.auth", "true");
            //props.put("mail.smtps.host", SMTP_HOST_NAME);
            //props.put(Email.MAIL_TRANSPORT_PROTOCOL, "smtps");

            final Session session =
JndiUtil.lookup(Config.JNDI_MAIL_SESSION)

            SimpleEmail email = new SimpleEmail() {

                @Override
                public void setMailSession(Session aSession) {
                    Class clazz = getClass();
                    try {
                        Field field = clazz.getField("session");
                        field.setAccessible(true);
                        field.set(this, session);
                    } catch (IllegalAccessException e) {
                        throw new ActionException(e);
                    } catch (IllegalArgumentException e) {
                        throw new ActionException(e);
                    } catch (NoSuchFieldException e) {
                        throw new ActionException(e);
                    } catch (SecurityException e) {
                        throw new ActionException(e);
                    }
                }

                @Override
                public Session getMailSession() throws EmailException {
                    return session;
                }

                @Override
                public String sendMimeMessage() throws EmailException {
                    Transport t = null;
                    try {
                        t = session.getTransport();
                        if (SMTP_AUTH_USER != null && SMTP_AUTH_PWD != null)
{
                            t.connect(SMTP_AUTH_USER, SMTP_AUTH_PWD);
                        } else {
                            t.connect();
                        }

                        t.sendMessage(message, message.getAllRecipients());
                        return message.getMessageID();
                    } catch (NoSuchProviderException e) {
                        throw new EmailException(e);
                    } catch (MessagingException e) {
                        throw new EmailException(e);
                    } finally {
                        if (t != null) {
                            try {
                                t.close();
                            } catch (MessagingException e) {
                                throw new EmailException(e);
                            }
                        }
                    }
                }
            };
            email.setMsg(BODY);
            email.addTo(TO);
            email.setFrom(FROM);
            email.setSubject(SUBJECT);
            email.send(); 
-- 
View this message in context: http://www.nabble.com/Commons-Email-1.1-with-Gmail-and-Provided-Mail-Session-tp24390884p24390884.html
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message