Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
sending smtp mail(gmail) using java mail (glassfish)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Pradyut  
View profile  
 More options Mar 11, 5:02 pm
From: Pradyut <prady...@gmail.com>
Date: Wed, 11 Mar 2009 14:02:56 -0700 (PDT)
Local: Wed, Mar 11 2009 5:02 pm
Subject: sending smtp mail(gmail) using java mail (glassfish)
Im sending a mail using the java mail API (supplied with glassfish
v2.1) and using glassfish server...

though the server does not matter but you may be have problems with
other servers....

Ok.. lets gets started ...
the screenshot from the glassfish admin console(the domain.xml file to
follow)

here we go....

the screenshot of the upper portion of the page....

http://picasaweb.google.com/pradyutb/OopPraddy?authkey=Gv1sRgCOKBjZTD...

the screenshot of the lower portion of the page....

http://picasaweb.google.com/pradyutb/OopPraddy?authkey=Gv1sRgCOKBjZTD...

your gmail password goes in the "mail-smtps-password" value field(left
empty)

in domain.xml the changes automatically made by the glassfish server
are: -

=========================================================================== ====
    <mail-resource debug="false" enabled="true"
from="prady...@gmail.com" host="smtp.gmail.com" jndi-
name="myMailServer" object-type="user" store-protocol="imap" store-
protocol-class="com.sun.mail.imap.IMAPStore" transport-protocol="smtp"
transport-protocol-class="com.sun.mail.smtp.SMTPTransport"
user="prady...@gmail.com">
      <description>no reply for pradyut.no-ip.org</description>
      <property name="mail-smtps-host" value="smtp.gmail.com"/>
      <property name="mail-smtps-password" value="your gmail password"/


      <property name="mail-smtps-socketFactory-class"
value="javax.net.ssl.SSLSocketFactory"/>
      <property name="mail-smtps-auth" value="true"/>
      <property name="mail-smtps-socketFactory-port" value="465"/>
      <property name="mail-smtps-port" value="465"/>
      <property name="mail-smtps-user" value="prady...@gmail.com"/>
      <property name="mail-smtps-starttls-enable" value="true"/>
      <property name="mail-smtps-socketFactory-fallback" value="false"/

    </mail-resource>
=========================================================================== ====

the code for sending the mail.... may be a servlet but i had written
in a java class...

--------------------------------------------------------------------------- -------------
@Resource(name = "myMailServer")
     private Session mailSession;
     //http://www.nabble.com/Glassfishv2u1-JavaMail-Session-and-Gmail-
problem-td18087187.html
     //http://markmail.org/message/o7ldjg54way5dswe

 public boolean sendMail(String recipient, String subject, String
text) {
     boolean exe = false;
     //javax.mail.Session mailSession = Session.getDefaultInstance
(props);

     Properties p = new Properties();
     //javax.mail.Session mailSession = Session.getInstance(p);

     InitialContext c = null;

     try
     {
          c = new InitialContext();
           //mailSession = (javax.mail.Session) c.lookup("java:comp/
env/myMailServer");
           mailSession = (javax.mail.Session) c.lookup("java:comp/env/
myMailServer");
           /*Object object = c.lookup("mail/MyMailServer");
           Session mailSession = null;
           if (object instanceof MailConfiguration)
             {
             MailConfiguration configuration = (MailConfiguration)
object;
             session = Session.getInstance
(configuration.getMailProperties());
             }
             else if (object instanceof Session)
             {
             session = (Session) object;
             }
            * */
     }
     catch(NamingException ne)
     {
         ne.printStackTrace();
     }

     //Properties props = mailSession.getProperties();
     /*
      String username = (String) props.get("mail.smtps.user");
     String password = (String) props.get("mail.smtps.password");
      */
     String username = "prady...@gmail.com";
     String password = "your gmail password";

     Transport transport = null;
     try
     {
         Message msg = new MimeMessage(mailSession);
         msg.setRecipients
(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
         msg.setSubject(subject);
         msg.setText(text);
         msg.saveChanges();

         //Transport.send(msg);
         transport = mailSession.getTransport("smtps");
         transport.connect(username, password);
         transport.sendMessage(msg, msg.getAllRecipients());
         exe = true;
     }
     catch (AddressException e)
     {
         e.printStackTrace();
         exe = false;
     }
     catch (MessagingException e)
     {
         e.printStackTrace();
         exe = false;
     }
     finally {
         try {
             if(transport != null)
                 transport.close();
         }
         catch(MessagingException me) {
             me.printStackTrace();
         }
         catch(Exception e) {
             e.printStackTrace();
         }
     }
     return exe;
 }
--------------------------------------------------------------------------- ------------------

notice the difference in the first line....
@Resource(name = "myMailServer")

and the context lookup....
mailSession = (javax.mail.Session) c.lookup("java:comp/env/
myMailServer");

no changes needed.. ony add "java:comp/env/" in context lookup....

hope you get through the code...
do reply comments or a better implementation...

thanks....

Pradyut
http://pradyut.tk
http://oop-edge.blogspot.com/
http://pradyutb.blogspot.com/
http://praddy-photos.blogspot.com/
http://oop-edge.spaces.live.com/
http://www.flickr.com/photos/praddy
http://groups.google.com/group/oop_programming
India


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google