• Entries (RSS)
  • Comments (RSS)

Sending an e-mail using WebSphere Mail session settings.

Posted by | Posted in WebSphere | Posted on 27-10-2008

Tagged Under : , , , ,

Sending an e-mail using WebSphere Mail session settings.

Yesterday I had posted about configuring a Mail Session in WebSphere Application Server admin console. Today I will be talking about sending an email from your JSP using the configured mail session.

To access the Mail session for sending your email you need to do a JNDI lookup from your JSP file. The complete code for sending email using the Mail Session settings is given below.

	// Look up mail session
	javax.naming.Context context = new javax.naming.InitialContext();
	javax.mail.Session mailSession = (javax.mail.Session) context.lookup("mail/localmail");
	javax.mail.Message msg = new javax.mail.internet.MimeMessage(mailSession);
	msg.setFrom(new javax.mail.internet.InternetAddress(fromEmail));
	msg.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(toEmail));
	msg.setSubject(mailSubject);
	msg.setText(mailBody);
 
	javax.mail.Transport.send(msg);
	System.out.println("Message Sent");

I have used the JNDI name directly for accessing the mail session settings. A better alternative is to use the resource reference in your JSP/Servlet code. The advantage of this approach is that we don’t need to alter the code even if our configuration name changes.

To add a resource reference opens your web.xml. Go to References Tab. Click on Add and select Resource Reference as the reference type and enter the details. For type select javax.mail.Session from the drop down box. If you are using resource reference, your lookup code will change to

	javax.mail.Session mailSession = (javax.mail.Session) context.lookup("java:comp/mail/localmail");

Share

Read More

Comments

7 comments posted onSending an e-mail using WebSphere Mail session settings.

  1. Great and very useful tutorial on a resource which is less told about. Thanks a lot. :razz:

  2. Hi this looks good.
    I tried this but having some problem.

    Before trying this option , I have a done the java mail program without using jndi naming. that works fine.
    now when I try to use the above methodology , I am getting an invalid address exception. Pls help
    I have pasted my error below

    [12/2/08 16:19:49:977 IST] 4a25f935 SystemOut O DEBUG SMTP RCVD: 550 5.7.1 Unable to relay for xx.bb@bank.com

    [12/2/08 16:19:49:977 IST] 4a25f935 SystemOut O Invalid Addresses
    [12/2/08 16:19:49:977 IST] 4a25f935 SystemOut O xx.bb@bank.com
    [12/2/08 16:19:49:977 IST] 4a25f935 SystemOut O DEBUG SMTPTransport: Sending failed because of invalid destination addresses
    [12/2/08 16:19:49:977 IST] 4a25f935 SystemOut O DEBUG SMTP SENT: RSET
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemOut O DEBUG SMTP RCVD: 250 2.0.0 Resetting

    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R javax.mail.SendFailedException: Sending failed;
    nested exception is:
    javax.mail.SendFailedException: Invalid Addresses;
    nested exception is:
    javax.mail.SendFailedException: 550 5.7.1 Unable to relay for xx.bb@bank.com

    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at javax.mail.Transport.send0(Transport.java:219)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at javax.mail.Transport.send(Transport.java:81)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.bac.esb.utilities.DataSourcePlugin.init(DataSourcePlugin.java:69)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:869)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at org.apache.struts.action.ActionServlet.init(ActionServlet.java:336)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at javax.servlet.GenericServlet.init(GenericServlet.java:258)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doInit(StrictServletInstance.java:82)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._init(StrictLifecycleServlet.java:147)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.servlet.PreInitializedServletState.init(StrictLifecycleServlet.java:270)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.init(StrictLifecycleServlet.java:113)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletInstance.init(ServletInstance.java:189)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at javax.servlet.GenericServlet.init(GenericServlet.java:258)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppServletManager.addServlet(WebAppServletManager.java:870)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadServlet(WebAppServletManager.java:224)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadAutoLoadServlets(WebAppServletManager.java:542)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.webapp.WebApp.loadServletManager(WebApp.java:1270)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.webapp.WebApp.init(WebApp.java:277)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.srt.WebGroup.loadWebApp(WebGroup.java:396)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.srt.WebGroup.init(WebGroup.java:216)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.webcontainer.WebContainer.addWebApplication(WebContainer.java:984)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.WebContainerImpl.install(WebContainerImpl.java:136)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.WebContainerImpl.start(WebContainerImpl.java:356)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:415)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:787)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:351)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:575)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:268)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:246)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:543)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:418)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ApplicationServerImpl.start(ApplicationServerImpl.java:117)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:543)
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:418)[12/2/08 16:19:50:196 IST] 4a25f935 SystemOut O DEBUG SMTP SENT: QUIT
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemOut O ** Invalid Addresses
    [12/2/08 16:19:50:196 IST] 4a25f935 SystemOut O xx.bb@bank.com

    [12/2/08 16:19:50:211 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.component.ServerImpl.start(ServerImpl.java:183)
    [12/2/08 16:19:50:211 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.WsServer.start(WsServer.java:128)
    [12/2/08 16:19:50:211 IST] 4a25f935 SystemErr R at com.ibm.ws.runtime.WsServer.main(WsServer.java:225)
    [12/2/08 16:19:50:211 IST] 4a25f935 SystemErr R at java.lang.reflect.Method.invoke(Native Method)
    [12/2/08 16:19:50:211 IST] 4a25f935 SystemErr R at com.ibm.ws.bootstrap.WSLauncher.main(WSLauncher.java:94)
    [12/2/08 16:19:50:211 IST] 4a25f935 SystemErr R at com.ibm.etools.websphere.tools.runner.api.ServerRunnerV5$1.run(ServerRunnerV5.java:97)

  3. Hi, I am new to using the Java mail.But have a requirment to send email notification from an MDB.This MDB typically listens on a queue.Everytime an error message is sent to the queue,the MDB onMessage should send across an email notification.I am working this out in WPS/WID 6.1.2.I configured the mail session from Process Server – Admin Console and tried using this code to send the mail from the onMessage method of the MDB but ends up in the erro below.

    nested exception is:
    class javax.mail.MessagingException: Exception reading response;
    nested exception is:
    java.net.SocketException: Connection reset
    [4/23/09 22:48:27:156 IST] 000000a5 SystemErr R javax.mail.SendFailedException: Sending failed;
    nested exception is:
    class javax.mail.MessagingException: Exception reading response;
    nested exception is:
    java.net.SocketException: Connection reset
    [4/23/09 22:48:27:156 IST] 000000a5 SystemErr R at javax.mail.Transport.send0(Transport.java:218)
    [4/23/09 22:48:27:156 IST] 000000a5 SystemErr R at javax.mail.Transport.send(Transport.java:80)

  4. @Sav, Not sure what exactly the problem is. May be a connectivity issue. Is your SMTP host name correct?

  5. Hi Albin, thanks for the suggestions in your mail. I checked that it was not the smtp server name but the firewall that was the issue.Disabled the firewall and it works fine.

  6. Cool, glad you got it working.

  7. Looking up the MailSession is ok with EJB 2.1 but using EJB3 I’d like to get the Session injected using something like:

    @Resource(name=”mail/DefaultMailSession”)
    privat Session mailSession;

    Did you ever try to get that working from within an EJB 3.0 Session Bean. I’am unable to because there is no way do define the Reference without having a DeploymentDeskriptor for my Beans. But EJB 3.0 doesn’t require any DD.

Post a Comment