JavaMail-javax.naming.NoInitialContextException:
I am trying to run a Java application to send SMTP mail through NetBeans 5.0/Sun Java System App Server 8.2/JavaMail 1.4.
I get the following exception:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
The code is:
/*
* Email.java
*
* Created on March 8, 2006, 1:12 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.apna.action;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class Email {
public static void main(String[] args) {
try{
new Email().sendMail("jitendravedi@gmail.com", "Good", "Testing");
}
catch(Exception e){ System.err.println(e);}
}
private void sendMail(java.lang.String email, java.lang.String subject, java.lang.String body) throws javax.naming.NamingException, javax.mail.MessagingException {
javax.mail.Session session = getSession();
javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session);
message.setSubject(subject);
message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(email, false));
message.setText(body);
javax.mail.Transport.send(message);
}
private javax.mail.Session getSession() throws javax.naming.NamingException {
javax.naming.Context c = new javax.naming.InitialContext();
return (javax.mail.Session) c.lookup("java:comp/env/mail/mail1");
}
}
Pls. help.
I don't quite understand how you're runing the application. Theapplication looks like a standalone application, not an applicationthat will run in the server. Are you running it as an applicationclient?
Thx for ur reply, bshannon.I am trying to send a simple text mail through NetBeans 5.0 & Sun Java App Server 8.2 by using JavaMail 1.4.Can u suggest a soln pls?
We're not communicating.
What kind of application are you writing? Is it a servlet? I don't think so.
Is it an EJB? I don't think so. Is it an application client that you deploy
to the application server? Possibly, but I can't tell. Or is it a simple
standalone application that actually doesn't need the application server
at all to run? Could be. But you have to tell me.
Once I understand what you're trying to do, I might be able to suggest
a solution to your problem.
For example, if all you really want to do is write a simple standalone
program, that doesn't need the application server, and that sends a
simple message, you don't need to use JNDI at all. Just copy the
demo programs that come with JavaMail.
I want to send a mail through a Struts Action class.
I am developing this module for deployment on a website.
So, I have to use the Sun App Server & JNDI.
I have setup the JavaMail resource on NetBeans & registered it for the Sun App Server.
What next do I need to do? Can u pls provide the code for sending mail?
Further, how do I run the Action Servlet(that will be sending the mail) on Sun App Server?
Thx for ur help so far.
Hi Jitendra
Although you are not completely answering Bill's questions, I am "guessing" you are not using an Java EE Application Client but a standard java program.
You should look at this documentation on the flags you need to pass to the JVM in order to have JNDI lookups working :
http://docs.sun.com/app/docs/doc/819-2556/6n4rap8rn?a=view
HTH, Jerome
Jerome's pointer will certainly help if your program is not actually running
in the server. But I couldn't tell from what you said whether the program
you're testing is running in the server or not. The program looks
like a standalone program, but you say you want it to run as a Struts
Action class. If you actually are running it as a Struts Action class,
I'm not sure why you're getting that exception.
Thx a lot-I am sorry I wasn't clear enough earlier.Here's what I want:I want to send mail using a Struts Action class in a Web app.I am using Sun App Server 8.2 & NetBeans 5.0.Can u pls help me?
Sorry, I have no idea why it's failing for you. My guess is that you're
doing something wrong, but from what little you've told me it's hard to
guess what you might be doing wrong.
It's important to note that the problem you're having has nothing to do
with JavaMail. You're not even getting far enough to use any JavaMail
classes before you run into trouble.
What I still don't understand is how you're running your program.
Your program has a static main method, is that what Struts is calling?
(Sorry, I'm not a Struts expert.) Or are you running that class as a
standalone program in NetBeans? If so, that would definitely cause
your problem.
My suggestion is to try to reproduce your problem with the simplest
application possible. Start by getting rid of Struts entirely and create
a very simple web app that includes your mail sending code. Deploy
it and run it without NetBeans if possible, in case that's confusing things.
Hope that helps.
I took ur advice and tried to send mail through a stand-alone java application using JavaMail 1.4. I need an SMTP server for this.
I want to set up my gmail ac on Outlook?
I am able to setup the ac. However, when I try sending/receiving mails, I get the following error:
Task 'www.gmail.com: Folder:Inbox Synchronizing headers.' reported error (0x80070057) : 'An unknown error has occurred. Please save any existing work and restart the program.'
Any ideas?
Can I send mail through GMail by using JavaMail? If yes, then what do I need to do to enable this?
The original problem here is a JNDI probelm nothing to do with javamail. To have this lookup() call succeed, you need to have the correct deployment descriptor stuff set up.
In my experience it is often a good idea to create two Contexts, for example here is a JNDI fragment to extract userid and password:
String userId;
String password
try {
Context initial = new InitialContext();
Context context = (Context) initial.lookup("java:comp/env");
userId = (String) context.lookup("userId");
password = (String) context.lookup("password");
} catch (NamingException e) { ... }
The web.xml that corresponds to this contains (among lots of other stuff) the following:
<env-entry>
<description>User ID to login to Oracle</description>
<env-entry-name>userId</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>myuid</env-entry-value>
</env-entry>
<env-entry>
<description>Password to login to Oracle</description>
<env-entry-name>password</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>mypswd</env-entry-value>
</env-entry>
There are more specific types of JNDI for datasources and ejb stuff, but these are the simplest generic environment use of JNDI. You will always get NamingExceptions if your environment is not set up to return the lookups correctly.
To get more information on this, read the J2EE spec... :-) It's all there.
Chris
