users@glassfish.java.net

Re: Send email to Gmail in glassfish

From: Sreeram Duvur <Sreeram.Duvur_at_Sun.COM>
Date: Thu, 24 Sep 2009 12:10:55 -0700

Hello,

I think you have to specify a SMTPS port# in connect call.

Here is some code that worked for me. Feel free to reuse.

  @Resource(name = "foo")
     javax.mail.Session session; // this javamail resource foo is
defined as a jndi-resource via Admin Console

     public boolean sendMessage(String name, String gateway) {
         try {
             Message msg = new MimeMessage(session);

             final Address[] recipientAddresses =
InternetAddress.parse(name + "@" + gateway);
             msg.setRecipients(Message.RecipientType.TO,
recipientAddresses);
              
msg.setRecipients(Message.RecipientType.TO,recipientAddresses);
             msg.setSubject("Welcome to Glassbook!");
             msg.setSentDate(new Date());
             msg.setText("Test");

             Transport transport = session.getTransport("smtps");
             transport.connect("smtp.gmail.com",465, "abc", "xxx"); //
server,port,username,password
             transport.sendMessage(msg, recipientAddresses);
             System.out.println("Message sent to: " +
recipientAddresses[0].toString());
         } catch (Exception e) {
             e.printStackTrace();
             return false;
         }
         return true;
     }


Create the resource JavaMail Session as:

JNDI Name: foo
Mailhost: smtp.gmail.com
Default User: your gmail account id
Default Return Address: <yourid>@gmail.com
Store Protocol: imap
Store Protocol Class: com.sun.mail.imap.IMAPStore
Transport protocl: smtp
Transport Protocol Class: com.sun.mail.smtp.SMTPTransport

HTH

Sreeram

On Sep 24, 2009, at 10:49 AM, 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
>