dev@glassfish.java.net

Re: Javamail/JNDI help?

From: Gary Horton <Gary.Horton_at_Sun.COM>
Date: Tue, 03 Jul 2007 09:06:05 -0600

sigh. Thanks anyway, Sahoo. Nobody seems to know how to deal with this -
I've even pinged the tomcat-users alias and ... no response.

Then for now, I'll just use the roll-my-own-session approach, supplying
a username/password...until I get some better insight. If I break
through with something, I'll post the list.

-gh

Sahoo wrote:
> Hi Gary,
>
> Sorry, I don't know how to get around the authentication issue.
>
> Thanks,
> Sahoo
>
> Gary Horton wrote:
>> Thanks Sahoo for the pointer. You are correct, this is a necessary
>> step. But, now I wonder how to get around an authentication issue -
>> i.e. I receive messages like "530 5.7.0 No AUTH command has been
>> given". To get around this, it appears I must use this type of
>> approach (creating my own Session):
>>
>> Properties props = new Properties();
>> props.put("mail.smtp.host", host);
>> props.put("mail.smtp.auth", "true");
>> Session sess = javax.mail.Session.getInstance(props, null);
>> Message msg = new MimeMessage(sess);
>> msg.setFrom(from);
>> msg.setRecipients(Message.RecipientType.TO, to);
>> msg.setSubject(subj);
>> msg.setSentDate(new Date());
>> msg.setText(body);
>> Transport t = null;
>> try {
>> t = sess.getTransport("smtp");
>> t.connect(user, password);
>> t.sendMessage(msg, msg.getAllRecipients());
>> this.logger.info("message sent!");
>> } finally { t.close(); }
>>
>> But this has two downsides, (1) I have to maintain and encrypt or
>> otherwise protect the password and (2) now I'm not leveraging the
>> container, I'm just using a standalone javamail session. Am I
>> misunderstanding something? I've also tried this without success,
>> where the session was obtained via the container:
>>
>> session.setPasswordAuthentication(
>> new URLName("smtp://mail-amer.sun.com"),
>> new PasswordAuthentication(info.getUserName(),
>> password));
>> Transport t = session.getTransport(protocol);
>> try {
>> t.connect();
>> t.sendMessage(message, message.getAllRecipients());
>> } finally {
>> t.close();
>> }
>>
>> But still no luck.
>>
>> Thanks Sahoo (and/or anyone else who wants to help) -
>> -gh
>>
>> Sahoo wrote:
>>> Hi Gary,
>>>
>>> You should first define a resource reference and associate the JNDI
>>> name
>>> with the resource reference. In your code, you should use the resource
>>> reference. Here are a couple of ways of doing this:
>>>
>>> 1. Using web.xml:
>>> ------------------------
>>> Add the following lines to your web.xml:
>>> <!-- Here the name of the resource reference is mail/foo and it is
>>> associated with the MailSession with global JNDI name as
>>> mail/awardsNotification-->
>>> <resource-ref>
>>> <res-ref-name>mail/foo</res-ref-name>
>>> <res-type>javax.mail.Session</res-type>
>>> <res-auth>Container</res-auth>
>>> <res-sharing-scope>Shareable</res-sharing-scope>
>>> <mapped-name>mail/awardsNotification</mapped-name>
>>> </resource-ref>
>>>
>>> Now your code looks like this:
>>>
>>> javax.mail.Session s = (javax.mail.Session) new
>>> InitialContext("java:comp/env/mail/foo");
>>>
>>> 2. Using dependency injection:
>>> ---------------------------------------
>>> If you are using Java EE 5 features, then you don't have to worry about
>>> web.xml or JNDI look up. Simply write the following line in your
>>> Servlet
>>> or managed web component:
>>> @Resource(name="mail/foo", mappedName="mail/awardsNotification")
>>> private javax.mail.Session s;
>>>
>>> Hope this helps.
>>>
>>> Thanks,
>>> Sahoo
>>>
>>> p.s.:
>>> A complete Java EE application that uses Mail session is available at
>>> http://weblogs.java.net/blog/ss141213/foss_2006_demo/VisitorRegistration_src.zip.
>>>
>>> Unzip and take a look at VisitorRegistrationService.java, which uses
>>> resource injection to inject a mail session. The injected Session is
>>> used inside sendMail() in that EJB. The application is described at
>>> http://weblogs.java.net/blog/ss141213/archive/2006/11/welcome_to_glas.html.
>>>
>>> The injection code and sendMail() should also work for a web app with
>>> version > 2.5.
>>>
>>>
>>> Gary Horton wrote:
>>>> Ken Paulsen has pointed me here to ask for help wrt correctly
>>>> setting up a JavaMail session in glassfish. I have set up the
>>>> session, but cannot successfully locate the resource using JNDI.
>>>>
>>>> My goal is to send out a simple text message via javamail from
>>>> my webapp, deployed to glassfish. From reading the Admin manual
>>>> (http://docs.sun.com/app/docs/doc/819-3658/6n5s5nkm4?a=view), I
>>>> configure JavaMail sessions with the following JNDI names:
>>>>
>>>> comp/env/mail/awardsNotification
>>>> mail/awardsNotification
>>>> awardsNotification
>>>>
>>>> I used several variations because my initial attempt using just
>>>> the 1st one (comp/env/mail/awardsNotification) didn't work out, so
>>>> I thought I must have been approaching the naming incorrectly.
>>>>
>>>> I then put together some test code that in effect does what the
>>>> glassfish manual describes
>>>> (https://glassfish.dev.java.net/javaee5/docs/DG/beaow.html):
>>>>
>>>> private Session getMailSession(String jndiName) {
>>>> try {
>>>> InitialContext ic = new InitialContext();
>>>> logger.info(jndiName + "...");
>>>> Session session = (Session)ic.lookup(jndiName);
>>>> logger.info("Success!");
>>>> return session;
>>>> } catch (Exception ex) {
>>>> this.logger.error("Failed: " + ex);
>>>> return null;
>>>> }
>>>> }
>>>>
>>>> Then I try it out finding any of the 3 JNDI resources with each of
>>>> the following JNDI name arguments, but they all fail. Here are the
>>>> arguments sent to the above method:
>>>>
>>>> 1. mail:comp/env/mail/awardsNotification
>>>> 2. java:comp/env/mail/awardsNotification
>>>> 3. java:mail/awardsNotification
>>>> 4. mail:mail/awardsNotification
>>>> 5. comp/env/mail/awardsNotification
>>>> 6. mail/awardsNotification
>>>> 7. mail:awardsNotification
>>>> 8. awardsNotification
>>>>
>>>> The output generated from these tests tell me, when I use #5, #6 or
>>>> #8, that I'm getting back a MailConfiguration object instead of a
>>>> Session object. I believe this is the problem, but I have no clue
>>>> how to remedy this.
>>>>
>>>> Here's the detailed output from invoking the getMailSession method
>>>> with the above arguments:
>>>>
>>>> 1. mail:comp/env/mail/awardsNotification...Failed: null
>>>> 2. java:comp/env/mail/awardsNotification...Failed: No object bound
>>>> to name java:comp/env/mail/awardsNotification
>>>> 3. java:mail/awardsNotification...Failed: No object bound to name
>>>> java:mail/awardsNotification
>>>> 4. mail:mail/awardsNotification...Failed: null
>>>> 5. comp/env/mail/awardsNotification...Failed:
>>>> java.lang.ClassCastException:
>>>> com.sun.enterprise.deployment.MailConfiguration
>>>> 6. mail/awardsNotification...Failed: java.lang.ClassCastException:
>>>> 7. mail:awardsNotification...Failed: mail:awardsNotification not
>>>> found
>>>> 8. awardsNotification...Failed: java.lang.ClassCastException:
>>>> com.sun.enterprise.deployment.MailConfiguration
>>>>
>>>> Can someone tell me what I'm doing wrong? Thanks in advance - and
>>>> if you would, please cc me in your reply, since I'm not on this
>>>> alias --
>>>> -gh
>>>