| |
oop_programming |
Im sending a mail using the java mail API (supplied with glassfish
though the server does not matter but you may be have problems with
Ok.. lets gets started ...
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
in domain.xml the changes automatically made by the glassfish server
=========================================================================== ====
the code for sending the mail.... may be a servlet but i had written
--------------------------------------------------------------------------- -------------
public boolean sendMail(String recipient, String subject, String
Properties p = new Properties();
InitialContext c = null;
try
//Properties props = mailSession.getProperties();
Transport transport = null;
//Transport.send(msg);
notice the difference in the first line....
and the context lookup....
no changes needed.. ony add "java:comp/env/" in context lookup....
hope you get through the code...
thanks....
Pradyut
v2.1) and using glassfish server...
other servers....
the screenshot from the glassfish admin console(the domain.xml file to
follow)
empty)
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"/
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"/
=========================================================================== ====
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
text) {
boolean exe = false;
//javax.mail.Session mailSession = Session.getDefaultInstance
(props);
//javax.mail.Session mailSession = Session.getInstance(p);
{
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();
}
/*
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";
try
{
Message msg = new MimeMessage(mailSession);
msg.setRecipients
(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
msg.setSubject(subject);
msg.setText(text);
msg.saveChanges();
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;
}
--------------------------------------------------------------------------- ------------------
@Resource(name = "myMailServer")
mailSession = (javax.mail.Session) c.lookup("java:comp/env/
myMailServer");
do reply comments or a better implementation...
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