Skip to Content
Recently we came across a scenario in which I have to send mails on the click of a button. But it was without an interface. So I thought about working the same with an interface.

Activation.jar and Mail.jar are the jar files that we need to add. You can find these files under C:\Program Files\SAP\JDT\eclipse\plugins\com.tssap.ext.libs.j2ee_1.3\lib

The user interface can be developed by using any layouts. I have choosen GridLayout.

public void doProcessBeforeOutput() throws PageException {

Form myForm = this.getForm(); // get the form from DynPage
// create your GUI here….
Label to=new Label(“To :”);
Label sub=new Label(“Sub :”);
InputField to_address=new InputField(“to_address”);
InputField subject=new InputField(“subject”);

GridLayout gd = new GridLayout();
gd.setId(“grid”);
gd.setCellSpacing(10);
gd.setWidth(“80%”);
gd.setDebugMode(true);

GridLayoutCell c11=new GridLayoutCell(“c11”);
c11.setWidth(“10%”);
c11.setHAlignment(CellHAlign.RIGHT);
c11.setContent(to);
gd.addCell(1,1,c11);

———————

myForm.addComponent(gd);
TextEdit body=new TextEdit(“body”);
body.setRows(7);
body.setCols(75);

GridLayout gd1 = new GridLayout();
gd1.setId(“grid1”);
————————–

myForm.addComponent(gd1);

Button send=new Button(“send”);
send.setText(“Send Mail!”);
send.setOnClick(“onSendMail”);

Button clear=new Button(“clear”);
clear.setText(“Clear”);
clear.setOnClick(“onClearTxt”);

GridLayout gd2 = new GridLayout();
gd2.setId(“grid2”);
————————————–
myForm.addComponent(gd2);

}

image

The following is the code for event handler

public void onSendMail(Event e) throws PageException {
Form fm=this.getForm();
InputField to=(InputField)this.getPageContext().getComponentForId(“to_address”);
String tostr=to.getValue().toString();
InputField sub=(InputField)this.getPageContext().getComponentForId(“subject”);
String subject=sub.getValue().toString();

TextEdit body=(TextEdit)this.getPageContext().getComponentForId(“body”);
String bodystr=sub.getValue().toString();
try{
IPortalComponentRequest request = (IPortalComponentRequest) this.getRequest();
IUser user=request.getUser();
// Mailing
Properties p=new Properties();
p.put(“mail.smtp.host”,”ServerName”);

Session s = Session.getInstance(p);
s.setDebug(false);

MimeMessage m = new MimeMessage(s);
InternetAddress fromAddress = new InternetAddress(user.getEmail());
InternetAddress toAddress = new InternetAddress(tostr);

m.setFrom(fromAddress);
m.setRecipient(Message.RecipientType.TO,toAddress);
m.setSubject(subject);
m.setContent(bodystr,”text/plain”);
Transport.send(m);
}catch(Exception ex) {
fm.addRawText(ex.toString());
}
}

The imports we need to organise are

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sap.security.api.IUser;
import com.sapportals.htmlb.Button;
import com.sapportals.htmlb.Form;
import com.sapportals.htmlb.GridLayout;
import com.sapportals.htmlb.GridLayoutCell;
import com.sapportals.htmlb.InputField;
import com.sapportals.htmlb.Label;
import com.sapportals.htmlb.TextEdit;
import com.sapportals.htmlb.enum.CellHAlign;
import com.sapportals.htmlb.event.Event;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.prt.component.IPortalComponentRequest;

Now we can deploy and run the application.

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Detlev Beutner
    Hi George,
    one very important rule in web development is “never send mails synchronously”. Why!? For different reasons: First, it takes too much time in a request/response cycle (a synchronous mail transfer needs about 1/2 second or more!). Second, what happens if the mail server you’re using is down? An exception is thrown, the mail is lost. A standard and more or less simple solution is to implement a scheduler task (timer / timer task) which sends mails in a different thread. When failing, it just tries again one hour later or so. Within a J2EE system, one often has the choice between an own created thread and using some scheduler service provided by the server (a bit more work).
    Beneath this, the question stays what JavaMail implementation should be used. It is a common mistake to deploy JavaMail a second (third, fourth…) time on the server instead of referring to the J2EE implementation (JavaMail is part of J2EE!). See /thread/41966 [original link is broken] for details.
    Last but not least within EP environment, especially if KM is used, there are more possibilities to send mail (and to use preconfigured mail server etc). This is also mentioned in the thread given above as well as here: /thread/25456 [original link is broken]
    Best regards, Detlev
    (0) 
    1. Hi Detlev,

      Thank very much for your reply. It was very valuable one for a begineer like me.

      George

      (0) 
  2. Hello george,
    Your BLOG is quiet help fule

    Keep up the good work!

    Thanks and Regards
    Pradeep Bhojak

    (0) 
  3. Former Member
    Hi,
    i have tried ur code to send a mail, but i am getting the exception as “javax.mail.NoSuchProviderException: smtp”.
    What are the settings do i keep?
    do u have any idea?

    Regards,
    sireesha.

    (0) 
  4. Former Member
    Hi,

    I need to send mail in which i can attach any file  attachment.so can u tell me which steps we have to set for fullfill my requirment.

    Regards,
    Gurprit Bhatia

    (0) 

Leave a Reply