on 05-20-2007 10:01 AM
Hi Experts,
I am having a webdynpro application that is used to send mails. Now I want to have a dropdown list for the 'To' input field. This will have the list of all email addresses in the active directory. Can any body suggest me how do i go about it.
Regards
Abdullah
HI,
Its possible with UME API, just go through the below, you will get some idea.
package com.email;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import com.sap.security.api.IGroup;
import com.sap.security.api.IRoleFactory;
import com.sap.security.api.IUser;
import com.sap.security.api.IUserAccountFactory;
import com.sap.security.api.IUserFactory;
import com.sap.security.api.UMException;
import com.sap.security.api.UMFactory;
import com.sapportals.htmlb.DropdownListBox;
import com.sapportals.htmlb.HtmlEdit;
import com.sapportals.htmlb.InputField;
import com.sapportals.htmlb.event.Event;
import com.sapportals.htmlb.event.TableNavigationEvent;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.htmlb.table.DefaultTableViewModel;
import com.sapportals.htmlb.table.TableColumn;
import com.sapportals.htmlb.table.TableView;
import com.sapportals.portal.prt.component.IPortalComponentContext;
import com.sapportals.portal.prt.component.IPortalComponentProfile;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentSession;
public class DropDownComp extends PageProcessorComponent {
public DynPage getPage() {
return new DropDownCompDynPage();
}
public static class DropDownCompDynPage extends JSPDynPage {
private final static int INITIAL_STATE = 0;
private final static int WELCOME_STATE = 1;
private final static int MAIL_STATE = 2;
private int state = INITIAL_STATE;
private String groupName = "";
private String selectedRole;
private IUserAccountFactory acctFac;
private IUserFactory userFac;
private IRoleFactory roleFactory;
private boolean[] selectedRows;
private String emailId = "";
private String mailBody = "";
private String subject = "";
private String cc = "";
// an instance of UserDetailBean to hold one record of users info
private UserDetailBean detailBean;
public TableView table;
private RoleBean myBean = null;
private DetailTableModel myModel = null;
IPortalComponentRequest request = null;
IPortalComponentContext context = null;
// (IPortalComponentContext) request.getComponentContext();
IPortalComponentSession session = null;
//request.getComponentSession();
public void doInitialization() {
IPortalComponentProfile profile =
((IPortalComponentRequest) getRequest())
.getComponentContext()
.getProfile();
Object o = profile.getValue("myBean");
if (o == null || !(o instanceof RoleBean)) {
myBean = new RoleBean();
profile.putValue("myBean", myBean);
} else {
myBean = (RoleBean) o;
}
ListViewModel listView = new ListViewModel();
UserDetailBean userDetailBean = new UserDetailBean();
userDetailBean.setListModel(listView.getModel());
}
public void doProcessAfterInput() throws PageException {
InputField myInputField =
(InputField) getComponentByName("group_name_input");
if (myInputField != null) {
this.groupName = myInputField.getValueAsDataType().toString();
}
InputField id_input =
(InputField) getComponentByName("group_name_input");
if (myInputField != null) {
this.emailId = id_input.getValueAsDataType().toString();
}
InputField id_subject =
(InputField) getComponentByName("subject_input");
if (id_subject != null) {
this.subject = id_subject.getValueAsDataType().toString();
}
HtmlEdit htmlEdit = (HtmlEdit) getComponentByName("htmlEdit");
if (null != htmlEdit) {
this.mailBody = htmlEdit.getText().toString();
}
}
public void doProcessBeforeOutput() throws PageException {
switch (state) {
case INITIAL_STATE :
{
this.setJspName("DropDown.jsp");
break;
}
case WELCOME_STATE :
{
this.setJspName("DisplayUsers.jsp");
break;
}
case MAIL_STATE :
{
this.setJspName("SendMail.jsp");
break;
}
}
}
public void onNavigation(Event event) {
IPortalComponentSession session =
((IPortalComponentRequest) this.getRequest())
.getComponentSession();
// NAVIGATION - get the event to recover the actual position
TableNavigationEvent tne = (TableNavigationEvent) event;
UserDetailBean bean =
(UserDetailBean) session.getValue("TableViewModel");
// With the event we can use method getFirstVisibleRowAfter(); which gives
// us the actual position (after the event)
if (bean != null) {
bean.setVisibleRow(
new Integer(tne.getFirstVisibleRowAfter()).toString());
session.putValue("TableViewModel", bean);
}
state = WELCOME_STATE;
}
public void onSeacrhButtonClicked(Event event) throws PageException {
DropdownListBox rolesListBox =
(DropdownListBox) getComponentByName("roleList");
selectedRole = rolesListBox.getSelection();
IPortalComponentRequest request =
(IPortalComponentRequest) this.getRequest();
IPortalComponentContext context =
(IPortalComponentContext) request.getComponentContext();
IPortalComponentSession session = request.getComponentSession();
// DetailTableModel class contains methods to create TableViewModel when data is passed in
// the form of java.util.List
DefaultTableViewModel tableView = null;
List tableValues = null;
String tabSelected = "";
if (selectedRole != null ) {
try {
DetailTableModel detailTableModel = new DetailTableModel();
tableValues = getUsersForARole(selectedRole);
tableView = detailTableModel.createTable(tableValues);
tabSelected = "UserTab";
} catch (Exception e) {
}
}
if(groupName !=null){
try {
DetailTableModel detailTableModel = new DetailTableModel();
tableValues = getUsersForAGroup(groupName);
tableView = detailTableModel.createTable(tableValues);
tabSelected = "UserTab";
} catch (UMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
detailBean = new UserDetailBean();
detailBean.setModel(tableView);
populateBeanFromProfile(detailBean, context.getProfile());
session.putValue("TableViewModel", detailBean);
HttpSession httpSession = request.getServletRequest().getSession();
httpSession.setAttribute("UserDetailList", tableValues);
httpSession.setAttribute("TabDetails", tabSelected);
session.putValue("TableViewModel", detailBean);
state = WELCOME_STATE;
}
public void onSelectAll(Event event) throws PageException {
IPortalComponentRequest request =
(IPortalComponentRequest) this.getRequest();
IPortalComponentSession session = request.getComponentSession();
UserDetailBean ub =
(UserDetailBean) session.getValue("TableViewModel");
TableView table =
(TableView) this.getComponentByName("myTableView");
int k = table.getRowCount();
for (int i = 0; i <= k; i++) {
table.selectRow(i, true);
}
ub.setOldtableview(table);
session.putValue("TableViewModel", ub);
state = WELCOME_STATE;
}
public void onDeselectAll(Event event) throws PageException {
IPortalComponentRequest request =
(IPortalComponentRequest) this.getRequest();
IPortalComponentSession session = request.getComponentSession();
UserDetailBean ub =
(UserDetailBean) session.getValue("TableViewModel");
TableView table =
(TableView) this.getComponentByName("myTableView");
int k = table.getRowCount();
for (int i = 0; i <= k; i++) {
table.selectRow(i, false);
}
ub.setOldtableview(table);
session.putValue("TableViewModel", ub);
state = WELCOME_STATE;
}
public void onSendMail(Event event) throws PageException {
IPortalComponentRequest request =
(IPortalComponentRequest) this.getRequest();
IPortalComponentSession session = request.getComponentSession();
UserDetailBean ub =
(UserDetailBean) session.getValue("TableViewModel");
TableView table =
(TableView) this.getComponentByName("myTableView");
DefaultTableViewModel model = ub.getModel();
String colName = model.getColumnName(2);
StringBuffer userMailIds = new StringBuffer();
TableColumn emailIdColumn = new TableColumn(model, colName);
String mailIds = "";
for (int i = 1; i <= table.getRowCount(); i++) {
if (table.isRowSelected(i)) {
try {
userMailIds.append(
(model.getValueAt(i, 2).toString()) + ";");
ub.setMailIds(userMailIds.toString());
ub.setMessage("");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
ub.setMailIds("sdf");
break;
}
}
}
session.putValue("TableViewModel", ub);
state = MAIL_STATE;
}
public void onSendEMail(Event event) throws PageException {
IPortalComponentRequest request =
(IPortalComponentRequest) this.getRequest();
IPortalComponentSession session = request.getComponentSession();
UserDetailBean ub =
(UserDetailBean) session.getValue("TableViewModel");
emailId = ub.getMailIds();
cc = ub.getMailIds();
boolean isMailSent =
sendMailToUsers(emailId, mailBody, subject, cc);
if (isMailSent) {
ub.setMessage("Mail Sent Successfully!!!");
} else {
ub.setMessage("Error while sending mail!!!");
}
state = MAIL_STATE;
}
private void populateBeanFromProfile(
UserDetailBean bean,
IPortalComponentProfile profile) {
bean.setDesign(profile.getProperty("design"));
bean.setHeaderVisible(profile.getProperty("headerVisible"));
bean.setFillUpEmptyRows(profile.getProperty("fillUpEmptyRows"));
bean.setFooterVisible(profile.getProperty("footerVisible"));
bean.setHeaderText(profile.getProperty("headerText"));
bean.setNavigationMode(profile.getProperty("navigationMode"));
bean.setSelectionMode(profile.getProperty("selectionMode"));
bean.setVisibleRowCount(profile.getProperty("visibleRowCount"));
}
private List getUsersForARole(String selectedRole) throws UMException {
roleFactory = UMFactory.getRoleFactory();
userFac = UMFactory.getUserFactory();
List userValues = new ArrayList();
// getting the roles for a particular user
try {
UserDetailBean userInfo = null;
String[] allUsrs =
roleFactory.getUsersOfRole(selectedRole, false);
for (int i = 0; i < allUsrs.length; i++) {
userInfo = new UserDetailBean();
int index = allUsrs<i>.indexOf(":");
String logonId = "";
if (index >= 0) {
logonId = allUsrs<i>.substring(index + 1);
IUser user = userFac.getUserByLogonID(logonId);
userInfo.setName(user.getDisplayName());
userInfo.setAffiliation(user.getDepartment());
userInfo.setEmail(user.getEmail());
userInfo.setCompany(user.getCompany());
userInfo.setPhone(user.getTelephone());
userValues.add(userInfo);
}
}
} catch (Exception e) {
UserDetailBean userInfo = new UserDetailBean();
userInfo.setName("Exception");
userInfo.setEmail("Caught");
userInfo.setEmail("aa@bb.com");
userInfo.setPhone("Admin");
userValues.add(userInfo);
}
return userValues;
}
private List getUsersForAGroup(String groupName) throws UMException {
List userValues = new ArrayList();
UserDetailBean userInfo = new UserDetailBean();
IGroup group =
UMFactory.getGroupFactory().getGroupByUniqueName(groupName);
IUser user = null;
Iterator itr = group.getUserMembers(true);
while (itr.hasNext()) {
String temp = itr.next().toString();
user = UMFactory.getUserFactory().getUser(temp);
userInfo.setName(user.getDisplayName());
userInfo.setAffiliation(user.getDepartment());
userInfo.setEmail(user.getEmail());
userInfo.setCompany(user.getCompany());
userInfo.setPhone(user.getTelephone());
userValues.add(userInfo);
}
return userValues;
}
private boolean sendMailToUsers(
String to,
String mailBody,
String subject,
String cc) {
StringTokenizer toAddress = new StringTokenizer(to, ";");
StringTokenizer ccAddress = new StringTokenizer(cc, ";");
IUser user = UMFactory.getAuthenticator().getLoggedInUser();
String from = user.getEmail();
String host = "10.80.197.145";
// create some properties and get the default Session
try {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
if (toAddress.hasMoreElements()) {
if (toAddress != null) {
msg.addRecipient(
Message.RecipientType.CC,
new InternetAddress(toAddress.nextToken()));
}
}
if (ccAddress.hasMoreElements()) {
if (ccAddress != null) {
msg.addRecipient(
Message.RecipientType.CC,
new InternetAddress(ccAddress.nextToken()));
}
}
if (subject != null && subject.length() > 0) {
msg.setSubject(subject);
}
if ((mailBody != null) && (mailBody.length() > 0)) {
msg.setContent(mailBody, "text/plain");
} else {
msg.setContent("", "text/plain");
}
msg.setSentDate(new Date(System.currentTimeMillis()));
Transport.send(msg);
return true;
} catch (Exception mex) {
System.out.println("Error " + mex);
return false;
}
}
}
}
Regards,
Bala
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI Ismail,
I think you can use UME api's for fetching all emil addresses from Active directory.
hope this helps to u
With Regards
Naidu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
89 | |
10 | |
9 | |
9 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.