users@glassfish.java.net

Re: JavaMail sessions: still not working

From: Behrang Saeedzadeh <behrangsa_at_gmail.com>
Date: Sat, 11 Nov 2006 23:54:47 +0330

Hi again,

Looks like the problem is that Glassfish expects us to enter property
names in a dash-separated format (i.e mail-smtp-host instead of
mail.smtp.host).

Now I have solved the configuration problem. However, when I execute
the program, I get the following exception:

   javax.servlet.ServletException: javax.mail.AuthenticationFailedException

Seems like that I cannot replace

       Session session = Session.getDefaultInstance(props, new
       Authenticator() {
           protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
           }
       });

with

       session.setPasswordAuthentication(session.getTransport().getURLName(),
                   new PasswordAuthentication("x_at_gmail.com", "pass"));

Any ideas what should I use instead of this?

Regards,
Behi



On 11/11/06, Behrang Saeedzadeh <behrangsa_at_gmail.com> 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
>
> --
> "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
>


-- 
"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