users@glassfish.java.net

Re: JavaMail sessions: still not working

From: Masoud Kalali <kalali_at_gmail.com>
Date: Fri, 10 Nov 2006 22:18:44 +0330

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/