users@glassfish.java.net

Re: Send email to Gmail in glassfish

From: Felipe Gaúcho <fgaucho_at_gmail.com>
Date: Thu, 24 Sep 2009 20:12:05 +0200

I have this working here, and I can try to blog during the weekend..
if you are in desperate mode, you can dig in my raw code.. sorry the
lack of documentation, but it is the best I can offer :)

in summary:

1) create the JavaMail resource:

asadmin create-javamail-resource --interactive=true
--user=$ASADMIN_USER --mailhost=smtp.gmail.com
--mailuser=cejug.classifieds --fromaddress=cejug.classifieds_at_gmail.com
--enabled=true --description="e-Mail account used to confirm the
registration of the Arena PUJ users" --storeprotocol=imap
--storeprotocolclass=com.sun.mail.imap.IMAPStore --transprotocol smtp
--transprotocolclass com.sun.mail.smtp.SMTPSSLTransport --property
mail-smtp.user=cejug.classifieds_at_gmail.com:mail-smtp.port=465:mail-smtp.password=????:mail-smtp.auth=true:mail-smtp.socketFactory.fallback=false:mail-password=????:mail-smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory:mail-smtp.socketFactory.port=465:mail-smtp.starttls.enable=true
mail/arena

* change your resource and account login, resource JNDI, etc .. please
check the GF documentation about each parameter...

2) send the email:

here it is a complete example:
https://cejug-classifieds.dev.java.net/source/browse/cejug-classifieds/trunk/cejug-classifieds-login/src/main/java/net/java/dev/cejug/classifieds/login/jms/NotificationMailerBean.java?rev=1326&view=markup

* ok, I am using a silly trick.. but whatever, it do the job :) I am
waiting that day I will have time to review this, and I am also
dreaming that someone else do it before me :) eheh

good luck, and feel free to ask details here..




On Thu, Sep 24, 2009 at 7:49 PM, <glassfish_at_javadesktop.org> wrote:
> Hi, guys,
>
> I am having issues sending email to Gmail in glassfishv3-prelude. Here is the exception:
> INFO: DEBUG: setDebug: JavaMail version 1.4.1
> INFO: DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
> INFO: DEBUG SMTP: useEhlo true, useAuth true
> INFO: DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
> SEVERE: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
>  nested exception is:
>        java.net.SocketException: Unconnected sockets not implemented
> SEVERE:         at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
> SEVERE:         at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
> SEVERE:         at javax.mail.Service.connect(Service.java:288)
> SEVERE:         at javax.mail.Service.connect(Service.java:169)
> SEVERE:         at com.tekevolving.site.server.service.GoogleTest.send2(GoogleTest.java:53)
> SEVERE:         at com.tekevolving.site.server.service.GoogleTest.main(GoogleTest.java:32)
> SEVERE:         at com.tekevolving.site.server.service.AdminService.register(AdminService.java:107)
> SEVERE:         at com.tekevolving.site.server.presentation.pages.RegistrationPage.register(RegistrationPage.java:138)
> SEVERE:         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> SEVERE:         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> SEVERE:         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>
> Here my code, please note this is a static method being invoked in glassfish container. I didn't use any conainter's facility, and if I
> run the same static method from standalone java commmand line, it works. It looks like glassfish is in my way.
>
>        public static void send_test() {
>                Properties props = System.getProperties();
>                props.put("mail.smtps.host", "smtp.gmail.com");
>                props.put("mail.smtps.auth", "true");
>                Session session = Session.getInstance(props, null);
>                session.setDebug(true);
>                Message msg = new MimeMessage(session);
>                SMTPTransport t = null;
>                try {
>                        msg.setFrom();
>                        msg.setRecipients(Message.RecipientType.TO,
>                                        InternetAddress.parse("xxxx_at_gmail.com", false));
>                        msg.setSubject("test subject");
>                        msg.setText("test body");
>                        msg.setHeader("X-Mailer", "tester");
>                        msg.setSentDate(new Date());
>                        t = (SMTPTransport)session.getTransport("smtps");
>                        t.connect("smtp.gmail.com", "yyyyyy_at_gmail.com", "xxxxx");
>                } catch (MessagingException e) {
>                        e.printStackTrace();
>                } finally {
>                        try {
>                                if (t != null)
>                                        t.close();
>                        } catch (MessagingException e) {
>                                // TODO Auto-generated catch block
>                                e.printStackTrace();
>                        }
>                }
>                System.out.println("Email has been send!!!!");
>        }
>
> I noticed lots of people over internet asking issues with email in glassfish, I don't see any concrete answers....
>
> guys.. I really really need your help!!!
>
> Thanks a lot in advance!
> Lann
> [Message sent by forum member 'taolu2000' (taolu2000_at_gmail.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=365597
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>



-- 
Looking for a client application for this service:
http://fgaucho.dyndns.org:8080/arena-http/wadl