Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos
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);

}




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.
6 Comments