Hi Masoud,
Thanks for the response. I have sent two different code snippets to
the forum. One uses "Session.getDefaultInstance" and it works fine.
The problem is with the one that is configured as a JNDI resource.
I can use an ugly hack/workaround to make it work:
Context c = new InitialContext();
Session session = (Session) c.lookup("java:comp/env/mail/gmail");
Session actualSession = Session.getInstance(session.getProperties(), new
Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
And then by using the actualSession everything just works fine. The
problem is that I don't know how to assign an Authenticator to the
"session" object which is looked up via JNDI.
Best Regards,
Behi
On 11/10/06, Masoud Kalali <kalali_at_gmail.com> wrote:
> You may look at :
> http://www.velocityreviews.com/forums/t141237-send-smtp-mail-using-javamail-with-gmail-account.html
> and
> http://www.geekzone.co.nz/forums.asp?ForumId=4&TopicId=9052
> it completely explains how to use Gmail to send emails using JavaMail.
>
> Behrang Saeedzadeh wrote:
> > Hi,
> >
> > The following code snippet, sends an email from a person's GMail
> > account to itself.
> >
> > public static void sendSampleMail(final String username, final String
> > password) throws MessagingException {
> > Properties props = new Properties();
> > props.put("mail.smtp.host", "smtp.gmail.com");
> > props.put("mail.debug", "true");
> > props.put("mail.smtp.auth", "true");
> > props.put("mail.smtp.port", "465");
> > props.put("mail.smtp.socketFactory.port", "465");
> > props.put("mail.smtp.socketFactory.class",
> > "javax.net.ssl.SSLSocketFactory");
> > props.put("mail.smtp.socketFactory.fallback", "false");
> >
> > Session session = Session.getDefaultInstance(props, new
> > Authenticator() {
> > protected PasswordAuthentication getPasswordAuthentication() {
> > return new PasswordAuthentication(username, password);
> > }
> > });
> >
> > InternetAddress userAddress = new InternetAddress(username);
> > Message message = new MimeMessage(session);
> > message.setFrom(userAddress);
> > message.addRecipient(Message.RecipientType.TO, userAddress);
> > message.setSubject("JavaMail/GMail test");
> > message.setContent("This is a message from you to yourself
> > :-)", "text/plain");
> > Transport.send(message);
> > }
> >
> > Just enter "yourUsername_at_gmail.com" as the username, and your password
> > as the password and you can verify that it works...
> >
> > Now I wanted to create a JavaMail Session Resource in Glassfish
> > preconfigured with these properties and use it to send an email from
> > my test Web application. You can see a screen shot of the JavaMail
> > Session's setup at:
> >
> > http://files.myopera.com/behrangsa/files/glassfish_mail_1.JPG
> > http://files.myopera.com/behrangsa/files/glassfish_mail_2.JPG
> >
> > Here's the code that I use to lookup the JavaMail session and use it
> > to perform the same action:
> >
> > Context c = new InitialContext();
> > Session session = (Session)
> > c.lookup("java:comp/env/mail/gmail");
> >
> > session.setPasswordAuthentication(session.getTransport().getURLName(),
> > new PasswordAuthentication("x_at_gmail.com", "pass"));
> > InternetAddress userAddress = new
> > InternetAddress("x_at_gmail.com");
> > Message message = new MimeMessage(session);
> > message.setFrom(userAddress);
> > message.addRecipient(Message.RecipientType.TO, userAddress);
> > message.setSubject("JavaMail/GMail test");
> > message.setContent("This is a message from you to yourself
> > :-)", "text/plain");
> > Transport.send(message);
> >
> > When I run the above code, I get the following exception:
> >
> > javax.servlet.ServletException:
> > com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a
> > STARTTLS command first 15sm7996698wrl
> >
> > The problem is that the returned Session object is not populated with
> > the properties I have defined via the Admin GUI. This can be confirmed
> > by enumerating the Session's properties:
> >
> > Properties p = sess.getProperties();
> > Enumeration e = p.propertyNames();
> > while (e.hasMoreElements()) {
> > String elem = (String) e.nextElement();
> > out.print("<p>" + elem + " --- " + p.getProperty(elem) +
> > "</p>");
> > }
> >
> > Which returns:
> >
> > mail.from --- behrangsa_at_gmail.com
> > mail.debug --- false
> > mail.transport.protocol --- smtp
> > mail.user --- behrangsa_at_gmail.com
> > mail.imap.class --- com.sun.mail.imap.IMAPStore
> > mail.host --- smtp.gmail.com
> > mail.store.protocol --- imap
> > mail.smtp.class --- com.sun.mail.smtp.SMTPTransport
> >
> > Any ideas why the JavaMail session is not populated with the
> > configuration properties I have defined via the Admin GUI?
> >
> > A suggestion:
> > I think it would be better, instead of having the required "Mail Host"
> > field, to have two (probably required) fields, named "Outgoing Mail
> > Server" and "Incoming Mail Server", because it is common to have two
> > different mail servers one for outgoing mails (e.g. smtp.gmail.com)
> > and one for incoming mails (e.g. pop3.gmail.com).
> >
> > Thanks in advance,
> > Behi
> >
>
>
> --
> Regards , Masoud Kalali
> Software Engineer Nabh Inc.
> http://weblogs.java.net/blog/kalali/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>
--
"Science is a differential equation. Religion is a boundary condition"
- Alan Turing
Behrang Saeedzadeh
http://www.jroller.com/page/behrangsa
http://my.opera.com/behrangsa