Kalaimaan

Doing simple things to make great life

Posts Tagged ‘Exchange Server’

Create Calander Appointment, EWS, Exchanger server with java stub

Posted by kalaimaan on February 10, 2009

The below example is explain how to create Calander appointment in exchange server using Java stub


import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.adaptime.integration.EWSClient.Calendar.MsExchangeCalendarProperty;

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HttpTransportProperties.Authenticator;
import org.apache.commons.httpclient.protocol.Protocol;

import com.microsoft.schemas.exchange.services._2006.messages.CreateItemDocument;
import com.microsoft.schemas.exchange.services._2006.messages.CreateItemResponseDocument;
import com.microsoft.schemas.exchange.services._2006.messages.CreateItemType;
import com.microsoft.schemas.exchange.services._2006.messages.ExchangeWebServiceStub;
import com.microsoft.schemas.exchange.services._2006.messages.ItemInfoResponseMessageType;
import com.microsoft.schemas.exchange.services._2006.types.BodyType;
import com.microsoft.schemas.exchange.services._2006.types.BodyTypeType;
import com.microsoft.schemas.exchange.services._2006.types.CalendarItemCreateOrDeleteOperationType;
import com.microsoft.schemas.exchange.services._2006.types.CalendarItemType;
import com.microsoft.schemas.exchange.services._2006.types.DistinguishedFolderIdNameType;
import com.microsoft.schemas.exchange.services._2006.types.DistinguishedFolderIdType;
import com.microsoft.schemas.exchange.services._2006.types.ImportanceChoicesType;
import com.microsoft.schemas.exchange.services._2006.types.NonEmptyArrayOfAllItemsType;
import com.microsoft.schemas.exchange.services._2006.types.ResponseClassType;
import com.microsoft.schemas.exchange.services._2006.types.TargetFolderIdType;


public class EWSCreateCalender
{
ExchangeWebServiceStub exService;

private static String NTLM_AUT = “_NTLM_DIGEST_BASIC_AUTHENTICATION_”;

private static String NTLM_PRO = “NTLM”;

private static String HTTPS = “https”;

String exchangeURL = null;

String bodyText;
String subject;
Calendar scal;
Calendar ecal;
String loc;
int minRBS;

private String itemId = null;
private String changeKey = null;


public EWSCreateCalender(String userName, String passWord)
{

String exchangeURL = “https://Manager/EWS/Exchange.asmx”;
try
{
exService = new ExchangeWebServiceStub(null, exchangeURL);
}
catch (AxisFault e)
{
e.printStackTrace();
}

checkAuthenticate(userName, passWord);
}


/**
*
*
*/
private void checkAuthenticate(String userName, String passWord)
{
Protocol AlcHttpsProtocol = new Protocol(HTTPS, new EWSSSLSocketFactory(), 443);
Protocol.registerProtocol(HTTPS, AlcHttpsProtocol);

Options options = exService._getServiceClient().getOptions();
Authenticator authenticator = new Authenticator();

List<String> authScheme = new ArrayList<String>();
authScheme.add(NTLM_PRO);

authenticator.setAuthSchemes(authScheme);
authenticator.setUsername(userName);
authenticator.setPassword(passWord);
authenticator.setHost(“Manager”);

authenticator.setDomain(exchangeURL);

authenticator.setPort(Integer.parseInt(“443”));

options.setProperty(NTLM_AUT, authenticator);

options.setUserName(userName);

exService._getServiceClient().setOptions(options);
}


/**
*
*/
public void createCalendarAppointment(String bodyText,String subject,Calendar scal,    Calendar ecal,String loc,int minRBS)
{
this.bodyText = bodyText;
this.subject = subject;
this.scal = scal;
this.ecal = ecal;
this.loc = loc;
this.minRBS = minRBS;

// Create the appointment.
CalendarItemType appointment = CalendarItemType.Factory.newInstance();

// Create the CreateItem request.
CreateItemType createItemRequest = CreateItemType.Factory.newInstance();

appointment = prepareAppointment(appointment);

appointment.setItemClass(“IPM.Appointment”);

createItemRequest.setSendMeetingInvitations(CalendarItemCreateOrDeleteOperationType.SEND_TO_ALL_AND_SAVE_COPY);

// Identify the destination folder that will contain the appointment.
DistinguishedFolderIdType folder = DistinguishedFolderIdType.Factory.newInstance();
folder.setId(DistinguishedFolderIdNameType.CALENDAR);

// Create the array of items that will contain the appointment.
NonEmptyArrayOfAllItemsType arrayOfItems = NonEmptyArrayOfAllItemsType.Factory.newInstance();
CalendarItemType[] calender = new CalendarItemType[1];
calender[0] = CalendarItemType.Factory.newInstance();
arrayOfItems.setCalendarItemArray(calender);
arrayOfItems.setCalendarItemArray(0, appointment);

createItemRequest.setSavedItemFolderId(TargetFolderIdType.Factory.newInstance());
createItemRequest.getSavedItemFolderId().setDistinguishedFolderId(folder);
createItemRequest.setItems(arrayOfItems);

CreateItemDocument createItemDocument = CreateItemDocument.Factory.newInstance();
createItemDocument.setCreateItem(createItemRequest);

CreateItemResponseDocument createItemResponseDocument = null;

try
{
createItemResponseDocument = exService.CreateItem(createItemDocument, null, null, null, null);
}
catch (RemoteException e)
{
return;
}

ItemInfoResponseMessageType[] infoResponseMessageType = createItemResponseDocument.getCreateItemResponse()
.getResponseMessages().getCreateItemResponseMessageArray();

for (int i = 0; i < infoResponseMessageType.length; i++)
{
if (infoResponseMessageType[i].getResponseClass() == ResponseClassType.SUCCESS)
{
System.out.println(“Success”);
itemId =  infoResponseMessageType[i].getItems().getCalendarItemArray()[0].getItemId().getId();
changeKey = infoResponseMessageType[i].getItems().getCalendarItemArray()[0].getItemId().getChangeKey();
System.out.println(“ItemId : ” + itemId);
System.out.println(“Change Id : ” + changeKey);
}
else if (infoResponseMessageType[i].getResponseClass() == ResponseClassType.ERROR)
System.out.println(“Error”);
}
}


/**
*
* @param appointment
* @param property
* @param type
* @return
*/
private CalendarItemType prepareAppointment(CalendarItemType appointment)
{
if (bodyText != null && bodyText.length() > 0)
{
appointment.setBody(BodyType.Factory.newInstance());
appointment.getBody().setBodyType(BodyTypeType.HTML);
appointment.getBody().setStringValue(bodyText);
}

appointment.setImportance(ImportanceChoicesType.NORMAL);

if (subject != null && subject.trim().length() > 0)
appointment.setSubject(subject);
else
return null;

if (scal != null)
appointment.setStart(scal);
else
return null;

if (ecal != null)
appointment.setEnd(ecal);
else
return null;

if (loc != null && loc.length() > 0)
appointment.setLocation(loc);

appointment.setReminderIsSet(true);

if (minRBS > MsExchangeCalendarProperty.remainder_Null)
{
appointment.setReminderIsSet(Boolean.TRUE);
appointment.setReminderMinutesBeforeStart(minRBS);
}

appointment.setImportance(ImportanceChoicesType.NORMAL);

return appointment;
}

public static void main(String[] args)
{
EWSCreateCalender calender = new EWSCreateCalender(“kalaimaan”, “test01.” );
Calendar cals = Calendar.getInstance();
Calendar cale = Calendar.getInstance();
cals.set(2009, 02, 11, 10,30);
cale.set(2009, 02, 11, 12,00);
calender.createCalendarAppointment(“sample boby test”, “Tomorrow morning we have scheduled for seminer, come without fail”, cals, cale, “Chennai”, 30);
}
}

Advertisements

Posted in Exchange Server | Tagged: , , , | 12 Comments »

Retrive Email item from the folder, Exchange server web service java

Posted by kalaimaan on February 10, 2009

The below method is use to retrive the email message from the specific folder (like inbox, sentitem, …)

Retrive Email item from Exchange server

/**
*
*/
public String retriveEmailItems(String folderType)
{
String status = “Error”;

// Form the FindItem request.
FindItemType findItemRequest = FindItemType.Factory.newInstance();

// Define which item properties are returned in the response.
ItemResponseShapeType itemProperties = ItemResponseShapeType.Factory.newInstance();
itemProperties.setBaseShape(DefaultShapeNamesType.ID_ONLY);

findItemRequest.setItemShape(itemProperties);

DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = DistinguishedFolderIdType.Factory.newInstance();
DistinguishedFolderIdType folderIdType = com.microsoft.schemas.exchange.services._2006.types.DistinguishedFolderIdType.Factory
.newInstance();

if (folderType.equalsIgnoreCase(“Inbox”))
folderIdType.setId(DistinguishedFolderIdNameType.INBOX);
else if (folderType.equalsIgnoreCase(“SentItems”))
folderIdType.setId(DistinguishedFolderIdNameType.SENTITEMS);
else if (folderType.equalsIgnoreCase(“DeleteItems”))
folderIdType.setId(DistinguishedFolderIdNameType.DELETEDITEMS);
else if (folderType.equalsIgnoreCase(“Drafts”))
folderIdType.setId(DistinguishedFolderIdNameType.DRAFTS);

folderIDArray[0] = folderIdType;
folderIDArray[0].setMailbox(EmailAddressType.Factory.newInstance());

NonEmptyArrayOfBaseFolderIdsType arrayOfBaseFolderIdsType = NonEmptyArrayOfBaseFolderIdsType.Factory
.newInstance();
arrayOfBaseFolderIdsType.setDistinguishedFolderIdArray(folderIDArray);
findItemRequest.setParentFolderIds(arrayOfBaseFolderIdsType);

// Define the traversal type.
findItemRequest.setTraversal(ItemQueryTraversalType.SHALLOW);

FindItemDocument findItemDocument = FindItemDocument.Factory.newInstance();
findItemDocument.setFindItem(findItemRequest);

FindItemResponseDocument findItemResponseDocument = null;
try
{
findItemResponseDocument = exService.FindItem(findItemDocument, null, null, null, null);
}
catch (Exception e)
{
return status;
}

FindItemResponseType findItemResponse = findItemResponseDocument.getFindItemResponse();

// Access the response message.
ArrayOfResponseMessagesType responseMessages = findItemResponse.getResponseMessages();
ResponseMessageType[] rmta = responseMessages.getFindItemResponseMessageArray();

for (ResponseMessageType rmt : rmta)
{
// One FindItemResponseMessageType per folder searched.
FindItemResponseMessageType firmt = (FindItemResponseMessageType) rmt;

if (firmt.getResponseClass() == ResponseClassType.SUCCESS)
status = “Success”;
else if (firmt.getResponseClass() == ResponseClassType.ERROR)
status = “Error”;

if (firmt.getRootFolder() == null)
continue;

FindItemParentType fipt = firmt.getRootFolder();
Object obj = fipt.getItems();

// FindItem contains an array of items.
if (obj instanceof ArrayOfRealItemsType)
{
ArrayOfRealItemsType items = ((ArrayOfRealItemsType) obj);

if (items.getItemArray() != null)
{
for (MessageType it : items.getMessageArray())
{
MessageType messageType = (MessageType) it;

MsExchangeEmailResponse response = null;

MessageType mess = getEmailByItem(messageType.getItemId().getId(), messageType.getItemId()
.getChangeKey());
System.out.println(“Message :\n” + mess);
}
}
}
}

return status;
}

/**
*
* @param id
* @param changeKey
* @return
*/
private MessageType getEmailByItem(String id, String changeKey)
{
MessageType temp =  null;
// Form the GetItem request.
GetItemType getItemRequest = GetItemType.Factory.newInstance();

getItemRequest.setItemShape(ItemResponseShapeType.Factory.newInstance());

getItemRequest.getItemShape().setBaseShape(DefaultShapeNamesType.ALL_PROPERTIES);

ItemIdType[] itemIdType = new ItemIdType[1];
itemIdType[0] = ItemIdType.Factory.newInstance();
itemIdType[0].setId(id);
itemIdType[0].setChangeKey(changeKey);
NonEmptyArrayOfBaseItemIdsType[] arrayOfBaseItemIdsType = new NonEmptyArrayOfBaseItemIdsType[1];
arrayOfBaseItemIdsType[0] = NonEmptyArrayOfBaseItemIdsType.Factory.newInstance();

arrayOfBaseItemIdsType[0].setItemIdArray(itemIdType);

getItemRequest.setItemIds(arrayOfBaseItemIdsType[0]);

// Here is the call to exchange.
GetItemDocument getItemDocument = GetItemDocument.Factory.newInstance();
getItemDocument.setGetItem(getItemRequest);
GetItemResponseDocument getItemResponseDocument = null;

try
{
getItemResponseDocument = exService.GetItem(getItemDocument, null, null, null, null);
}
catch (RemoteException e)
{
return null;
}

GetItemResponseType getItemResponse = getItemResponseDocument.getGetItemResponse();

ItemInfoResponseMessageType getItemResponseMessage = (ItemInfoResponseMessageType) getItemResponse
.getResponseMessages().getGetItemResponseMessageArray(0);

if (getItemResponseMessage != null)
{
if (getItemResponseMessage.getResponseClass() == ResponseClassType.SUCCESS)
{
temp = (MessageType) getItemResponseMessage.getItems().getMessageArray(0);

}
}

return temp;
}

Posted in Exchange Server | Tagged: , , , , | Leave a Comment »

Delete Email item in Exchange Server EWS

Posted by kalaimaan on February 10, 2009

Delete Email Item In Exchange Server using Java Stub interface

The below code is use to delete the particular item of email

/**
*
* @param id
* @param key
* @return
*/
private String deleteMessageItem(String id, String key)
{
String status = “Error”;

DeleteItemType deleteItemType = DeleteItemType.Factory.newInstance();

NonEmptyArrayOfBaseItemIdsType arrayOfBaseItemIdsType = NonEmptyArrayOfBaseItemIdsType.Factory.newInstance();
ItemIdType[] itemIdType = new ItemIdType[1];
itemIdType[0] = ItemIdType.Factory.newInstance();
itemIdType[0].setId(id);
itemIdType[0].setChangeKey(key);
arrayOfBaseItemIdsType.setItemIdArray(itemIdType);
deleteItemType.setItemIds(arrayOfBaseItemIdsType);
deleteItemType.setDeleteType(DisposalType.HARD_DELETE);

DeleteItemDocument deleteItemDocument = DeleteItemDocument.Factory.newInstance();
deleteItemDocument.setDeleteItem(deleteItemType);

DeleteItemResponseDocument deleteItemResponseDocument = null;
try
{
deleteItemResponseDocument = exService.DeleteItem(deleteItemDocument, null, null, null, null);
}
catch (RemoteException e)
{
return status;
}

DeleteItemResponseType deleteItemResponseType = deleteItemResponseDocument.getDeleteItemResponse();

ResponseMessageType[] responseMessageType = deleteItemResponseType.getResponseMessages()
.getDeleteItemResponseMessageArray();

for (int i = 0; i < responseMessageType.length; i++)
{
if (responseMessageType[i].getResponseClass() == ResponseClassType.SUCCESS)
status= “Success”;
else if (responseMessageType[i].getResponseClass() == ResponseClassType.ERROR)
status= “Error”;
}

return status;
}

Posted in Exchange Server | Tagged: , , , , , | 1 Comment »

Exchange Server 2007 Web service, SOAP

Posted by kalaimaan on January 29, 2009

Hi,

I like the share my Exchanges web service knowledge. Check the steps to generate the webservice package, installation of Exchanges server and prerequisite details

Prerequisite installation

Installation Steps for Exchange Server 2007:

Step1: Install windows 2003 x64 Standard Edition with service pack 2.

Step2: Install IIS service using windows component.

Step3: Install Active directory with DNS configuration.

Step4: Install .Net Frame work 2.0 x64 with latest service pack.

Step5: Install Power Management shell x64

Step6: Install MMC Controller x64

Step7: Installing required windows patch

Step8: Setting required properties for install MS Exchange server 2007

Step9: Install Ms Exchange server 2007 standard Edition.

To install Active directory with Domain control.

  1. Click Start, click Run, type dcpromo, and then click OK.
  2. On the first page of the Active Directory Installation Wizard, click Next.
  3. On the next page of the Active Directory Installation Wizard, click Next.
  4. On the Domain Controller Type page, click Domain Controller for a new domain, and then click Next.
  5. On the Create New Domain page, click Domain in a new forest, and then click Next.
  6. On the New Domain Name page, in the Full DNS name for new domain box, type NewDomain.local, and then click Next.
  7. On the Database and Log Folders page, accept the defaults in the Database folder box and the Log folder box, and then click Next.
  8. On the Shared System Volume page, accept the default in the Folder location box, and then click Next.
  9. On the DNS Registration Diagnostics page, click Install and configure the DNS server on this computer and set this computer to use this DNS server as its preferred DNS Server, and then click Next.
  10. On the Permissions page, click Permissions compatible only with Windows 2000 or Windows Server 2003 operating systems, and then click Next.
  11. On the Directory Services Restore Mode Administrator Password page, enter a password in the Restore Mode Password box, retype the password to confirm it in the Confirm password box, and then click Next.
  12. On the Summary page, confirm the information is correct, and then click Next.
  13. When prompted to restart the computer, click Restart now.
  14. After the computer restarts, log on to Administrator.

To install IIS on Windows Server 2003

  1. Click Start, point to Control Panel, and then click Add or Remove Programs.
  2. In Add or Remove Programs, click Add/Remove Windows Components.
  3. In the Windows Components Wizard, under Components, select Application Server.

    Note:

    Selecting Application Server performs a default installation of Internet Information Services (IIS) and includes components that are not necessary for Certificate Services. In most cases, this installation is acceptable for an isolated test environment. However, if you plan to connect your test environment to your production network, consult your organization’s security policy to determine which components to install.
  4. Click Next.
  5. After the wizard completes the installation, click Finish

To Install Exchange server 2007 in windows 2003

Reference : Exchange Server 2007 Guide.odt

How to create Exchange server stub to access.

Step 1:

Download Axis2 1.3 version from Apache axis tomcat

Step 2:

Place the folder axis2-1.3 in c drive

Step 3:

We have to set the following path in Enviorenment variable and the below is the sample

Please set the correct path for the corresponding “Variable Name”

1. AXIS_HOME – C:\axis2-1.3

2. JAVA_HOME – C:\Program Files\Java\jdk1.5.0_05

3. TOMCAT_HOME – C:\Program Files\Apache Software Foundation\Tomcat 5.0

4. JWSDP_SHARED – C:\Program Files\Apache Software Foundation\Tomcat 5.0\jwsdp-shared;

5. ANT_HOME – D:\MsExchangeServer-WorkFolder\Axis – Java\apache-ant-1.7.0-bin\apache-ant-1.7.0

6. PATH – %AXIS2_HOME%\bin;%AXIS_HOME%\webapp; %ANT_HOME%\bin;%TOMCAT_HOME%\common\lib; %JWSDP_SHARED%\bin;%JWSDP_SHARED%\lib; %JWSDP_SHARED%\webapps\jwsdp-callback\WEB-INF\classes

Step 4:

In the folder axis2-1.3 we have a bin folder. Open the cmd window and enter into

the path “C:\axis2-1.3\bin.”

<wsdl:service name=”ExchangeService”>

<wsdl:port name=”ExchangeServicePort” binding=”tns:ExchangeServiceBinding”>

<soap:address location=”<ExchangeServer>”/>

</wsdl:port>

</wsdl:service>

Place the Excahange server Wsdl and XSd file under bin folder and run the below command using “cmd prompt”

wsdl2java -d xmlbeans -s -ss -g -o ews -uri Services.wsdl

you can generate the set of java package for Exchange web service. Generate the Stub using the package

Have a Good Day 🙂


Posted in Exchange Server | Tagged: , , , | 6 Comments »