sending smtp mail(gmail) using java mail (glassfish)

1,658 views
Skip to first unread message

Pradyut

unread,
Mar 11, 2009, 5:02:56 PM3/11/09
to oop_programming
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=Gv1sRgCOKBjZTD9db1bA&feat=directlink#5312015285602997474

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

http://picasaweb.google.com/pradyutb/OopPraddy?authkey=Gv1sRgCOKBjZTD9db1bA&feat=directlink#5312016739707868290

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="prad...@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="prad...@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="prad...@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 = "prad...@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 all
Reply to author
Forward
0 new messages