Skip to Content
Author's profile photo Former Member

Enabling Java Mail Functionality in Your Applications

There is a nice feature in the

SAP NetWeaver Application Server Java

, that can help you enhance your application but that for some reasons people are not aware of. Therefore, I have decided to write a blog about it and show you how you can add mailing functionality to your application.


SAP NetWeaver Application Server Java

provides a

JavamailClient service

, which ensures mail client features to the components running on the application server . As the J2EE specification requires , there is a


object that is bound to the the application servre’s naming envirnonment . This object can be looked up and used to send/check e-mails.

In your application, you need to lookup the  java:comp/env/mail/MailSession object and get  the  appropriate “transport” or “store” protocol , using the  Javamail API .


Here is a simple example how to send mail message  in a JSP code:
<textarea name=”JSP code” rows=”45″ cols=”90″ wrap=”virtual”>

<%@ page import=”*,
                 javax.mail.internet.*” %>

    Transport tr = null; 
    try {
      InitialContext ctx = new InitialContext();

      Session ses = ( Session )ctx.lookup(“java:comp/env/mail/MailSession”);
      tr = ses.getTransport( “smtp” ); 
      // or ses.getTransport( “smtps” ); for SSL connection
      tr.connect( “”, “user”, “pass” );

      MimeMessage message = new MimeMessage(ses);

      message.setSubject(“Test”, “UTF-8”);

      message.setContent(“This is test”, “text/html;charset=UTF-8”);

      message.setRecipients(javax.mail.Message.RecipientType.TO, “”);
      InternetAddress[] to = new InternetAddress[]{new InternetAddress(“”)};

      tr.sendMessage( message, message.getAllRecipients() );
      out.println(“h1. Successfully sent!
    } catch (Exception ex) {
      out.println(“<br><pre> Error:”);
      StringWriter strWr = new StringWriter();
      ex.printStackTrace(new PrintWriter(strWr));
    } finally {
      if (tr != null) {

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      A good attempt to explain something most people are not aware of. can you explain in detail ( explaining  little bit on how we develop this in NWDS ) with some screen shots on where to write the code and how do we test etc.,