Skip to Content
Author's profile photo Biplab Ray

Send E-Mail using Java

Applies To : JDK 1.5 / 1.6


In most of the application required to send auto generated mail from application.

  1. To notify the support team about their task.
  2. To notify management team about the fault of the application.

Author : Biplab Ray

Company : Tata Consultancy Services

Created on : 11th February 2015

Author Bio :

Biplab Ray is working for Tata Consultancy Services as Assistant Consultant and development of SAP EP , composite applications using CAF, BPM, BRM, WebDynpro for Java. He also works in technologies like Java/J2EE and has depth understanding on eSOA, Webservice, Enterprise Service, XML.

Implementation :

To solve the above issue, we should have a application via which the main application should able to post the e-mail.

Below is the implementation of the post email method.

In the class we have to import following java packages.

import javax.mail.Address;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

In the method body we should write the below logic

String stringSMTPHOST = “SmtpHost”;

String stringSMTPPORT = “SmtpPort”;

final String stringUSERSERVER = System.getProperty(“SAPSYSTEMNAME”).toUpperCase();

String string_AUTO_GENERATED_TEXT = “Auto_Generated_Text”;

Session session = null;

Properties props = new Properties();

       props.put(“”, stringSMTPHOST);

       props.put(“mail.smtp.socketFactory.port”, stringSMTPPORT);

       session = Session.getInstance(props);

List<Object of Data Structure> list_EmailStruct = “Email Address List”;

Address[] addressesTo = new Address[list_EmailStruc.size()];

Now based on the list of Email Address application need to iterate like below.

for(int i =0; i < list_EmailStruct.size(); i++){

String stringDataSource = “”;

String string_EmailID = “getEmailID from the list_EmailStruct”;

addressesTo[i] = new InternetAddress(string_EmailID);

String string_subject = “As per the business requirement”;

String string_Message = “getMessgaeDetails”;

       message.setFrom(new InternetAddress(stringUSERSERVER+”@”+”Domain Name Like”));




try {

      Transport.send(message); // With Help of send method of Transport API , our mail will get post

      } catch (Exception e) {




Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Jun Wu
      Jun Wu

      i think the solution you posted is not the proper way(regarding mail notification) in netweaver java server.

      sap has some standard way...

      Using JavaMail - SAP NetWeaver Composition Environment Library - SAP Library

      Author's profile photo Biplab Ray
      Biplab Ray
      Blog Post Author


      This is full custom solution of mail notification using java of SAP NetWeaver Application server. SAP AS java by default don't have any standard solution.