Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member185706
Participant
0 Kudos


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.




The

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

java:comp/env/mail/MailSession

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 .

</p>
<p>

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

<%@ page import="java.io.*,
                 javax.mail.*,
                 javax.naming.*,
                 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( "myHost.my", "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, "test1@myHost.my");
      InternetAddress[] to = new InternetAddress[]{new InternetAddress("bobby@myHost.my")};

      message.addFrom(to);
     
      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));
      out.println(strWr.toString()+"</pre>");
    } finally {
      if (tr != null) {
        tr.close();
      }
    }
%>
</textarea>
 

1 Comment