Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
Greenhorn
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 http://www.huihoo.org/jboss/online_manual/3.0/ch13s98.html

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

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

<mbean code="org.jboss.mail.MailService"
name="jboss:service=Mail">
<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 -->
<configuration>
<!-- Change to your mail server prototocol -->
<property name="mail.store.protocol" 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="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/>

<!-- Change to the SMTP gateway server -->
<!--<property name="mail.smtp.host" value="smtp.nosuchhost.nosuchdomain.com"/>-->
<property name="mail.smtp.host" value="smtp.gmail.com"/>

<!-- 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="nobody@nosuchhost.nosuchdomain.com"/>-->
<property name="mail.from" value="MAIL FROM ADDRESS"/>
<!--Added-->
<property name="mail.smtp.socketFactory.class" value="javax.net.ssl.SSLSocketFactory" />

<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"/>
</configuration>
</attribute>
<depends>jboss:service=Naming</depends>
</mbean>

</server>


My ejb-jar.xml:
<enterprise-beans>
<session>
<ejb-name>Mailer</ejb-name>
<local>sessionBean.MailerLocal</local>
<ejb-class>sessionBean.MailerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<resource-ref>
<res-ref-name>mail/Mail</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>

My jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>Mailer</ejb-name>
<jndi-name>Mailer</jndi-name>
</session>
</enterprise-beans>
<resource-managers>
<resource-manager>
<res-name>mail/Mail</res-name>
<res-jndi-name>Mail</res-jndi-name>
</resource-manager>
</resource-managers>
</jboss>

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) {
e.printStackTrace();
}

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");
Transport.send(m);
} catch (javax.mail.MessagingException e) {
e.printStackTrace();
}
}

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(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at sessionBean.MailerBean.sendMails(MailerBean.java:42)
at sessionBean.MailerBean.main(MailerBean.java:72)

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!