Kalaimaan

Doing simple things to make great life

Posts Tagged ‘retrive’

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

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

PHP with MySql Connectivity and Datamanipulation

Posted by kalaimaan on February 3, 2009

PHP with MySql Connectivity and Datamanipulation

The following example is describe the details of insert delete and view the datas from my sql using PHP script

Example:

<html>
<body>

<table width=”100%” border=”2″>
<tr><td><center>Welcome to PHP script</center></td></tr>
</table>

<?php

$Server = “localhost”;
$Username =”root”;
$Password = “admin”;

mysql_connect($Server,$Username, $Password) or die(mysql_error());
echo “Connected to MySQL<br />”;
mysql_select_db(“emp”) or die(mysql_error());
echo “Connected to Database<br>”;

printData();
insertData();
deleteData();

// insert details to the selected mysql table
// delete details to the selected mysql table
function printData()
{
// Select Details from the table
$result = mysql_query(“SELECT * FROM empm”) or die(mysql_error());

echo “<table border=’1′ width=’50%’>”;
echo “<tr> <th>Id</th> <th>Name </th> <th>Phone No. </th> </tr>”;

while($row = mysql_fetch_array( $result ))
{
echo “<tr><td>”;
echo $row[’empid’];
echo “</td><td>”;
echo $row[‘name’];
echo “</td><td>”;
echo $row[‘pno’];
echo “</td>”;
echo “</tr>”;
}

echo “</table>”;
}

function insertData()
{
// Insert a row of information into the table “example”
mysql_query(“INSERT INTO empm (empid,name,pno) VALUES(‘0006′,’Sri’,’98418′ ) “) or die(mysql_error());
echo “Data Inserted!”;
printData();
}

function deleteData()
{
mysql_query(“Delete from empm where empid =’0006′”) or die(mysql_error());
echo “Data Deleted!”;
printData();
}

?>

</body>

</html>

Screen Shot:screen1

Posted in PHP | Tagged: , , , , , , | Leave a Comment »