Kalaimaan

Doing simple things to make great life

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;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: