Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please Help!! Failed to send email from J2EE app, Jboss 4.2.3 server  RSS feed

Hoang Anh Le
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to send email from EJB SessionBean. I have been googling around, but could not send email successfully.

I followed some links like this

My mail-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: mail-service.xml 62349 2007-04-15 16:48:15Z $ -->

<!-- ==================================================================== -->
<!-- Mail Connection Factory -->
<!-- ==================================================================== -->
<classpath codebase="lib"
archives="mail.jar, activation.jar, mail-plugin.jar"/>

<mbean code="org.jboss.mail.MailService"
<attribute name="JNDIName">java:/Mail</attribute>
<attribute name="User">MY EMAIL USER NAME</attribute>
<attribute name="Password">MY EMAIL PASSWORD</attribute>
<attribute name="Configuration">
<!-- A test configuration -->
<!-- Change to your mail server prototocol -->
<property name="" value="pop3"/>
<property name="mail.transport.protocol" value="smtp"/>

<!-- Change to the user who will receive mail -->
<property name="mail.user" value="nobody"/>

<!-- Change to the mail server -->
<property name="" value=""/>

<!-- Change to the SMTP gateway server -->
<!--<property name="" value=""/>-->
<property name="" value=""/>

<!-- The mail server port -->
<!--<property name="mail.smtp.port" value="25"/>-->
<property name="mail.smtp.port" value="465"/>

<!-- Change to the address mail will be from -->
<!--<property name="mail.from" value=""/>-->
<property name="mail.from" value="MAIL FROM ADDRESS"/>
<property name="mail.smtp.socketFactory.class" value="" />

<property name="mail.smtp.starttls.enable" value="true" />
<!--End Added-->
<!-- Enable debugging output from the javamail classes -->
<!--<property name="mail.debug" value="false"/>-->
<property name="mail.debug" value="true"/>


My ejb-jar.xml:

My jboss.xml
<?xml version="1.0" encoding="UTF-8"?>

and in my SessionBean
public void sendMails(){
javax.mail.Session session = null;
try {
InitialContext initCtx = new InitialContext();
session = (javax.mail.Session)initCtx.lookup("java:/Mail");
} catch (javax.naming.NamingException e) {

try {
MimeMessage m = new MimeMessage(session);

Address[] to = new InternetAddress[] {new InternetAddress("SOME EMAIL ADDRESS")};

m.setRecipients(Message.RecipientType.TO, to);
m.setFrom(new InternetAddress("SOME EMAIL ADDRESS"));
m.setSubject("JavaMail Test");
m.setSentDate(new Date());
m.setContent("Test from inside EJB Using JBoss", "text/plain");
} catch (javax.mail.MessagingException e) {

and the error is :
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
at javax.naming.spi.NamingManager.getInitialContext(
at javax.naming.InitialContext.getDefaultInitCtx(
at javax.naming.InitialContext.getURLOrDefaultInitCtx(
at javax.naming.InitialContext.lookup(
at sessionBean.MailerBean.sendMails(
at sessionBean.MailerBean.main(

I guess it has something to do with jndi ... but I could not solve it. Please help!!

Sorry for a lengthy post. Thanks for your time!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!