Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access exchange e-mail in C#

Do you know if there's a way?

I've used this library to access a pop3 server, but it doesn't work with an exchange server.

Do you know of any other library or piece of code that'll show me how to do it?

I cannot change any settings on the server.

like image 884
juan Avatar asked May 14 '09 19:05

juan


People also ask

How do I access Exchange EMC?

On the desktop or the Start screen, press Windows key + Q. In the Search charm, type Exchange Management Shell. When the shortcut appears in the results, you can select it. On the desktop or the Start screen, move your cursor to the upper-right corner, or swipe left from the right edge of the screen to show the charms.

How do I access the Exchange?

To access the EAC in a web browser on the Exchange server itself, you can use the value https://localhost/ecp . External URL: By default, this value is unconfigured. Before you can connect to the EAC from the Internet, you need to configure the following settings: The external URL value on the ECP virtual directory.


1 Answers

If you use Exchange 2007 and have web services enabled, this is pretty easy. I added a 2.0-style classic Web Reference to my VS2008 project, and I can get mail messages like this:

// exchange 2007 lets us use web services to check mailboxes.
using (ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding())
{
    ICredentials creds = new NetworkCredential("user","password");
    exchangeServer.Credentials = creds;
    exchangeServer.Url = "https://myexchangeserver.com/EWS/Exchange.asmx";
    FindItemType findItemRequest = new FindItemType();
    findItemRequest.Traversal = ItemQueryTraversalType.Shallow;

    // define which item properties are returned in the response
    ItemResponseShapeType itemProperties = new ItemResponseShapeType();
    itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
    findItemRequest.ItemShape = itemProperties;

    // identify which folder to search
    DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
    folderIDArray[0] = new DistinguishedFolderIdType();
    folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;

    // add folders to request
    findItemRequest.ParentFolderIds = folderIDArray;

    // find the messages
    FindItemResponseType findItemResponse = exchangeServer.FindItem(findItemRequest);

    // read returned
    FindItemResponseMessageType folder = (FindItemResponseMessageType)findItemResponse.ResponseMessages.Items[0];
    ArrayOfRealItemsType folderContents = new ArrayOfRealItemsType();
    folderContents = (ArrayOfRealItemsType)folder.RootFolder.Item;
    ItemType[] items = folderContents.Items;

    // if no messages were found, then return null -- we're done
    if (items == null || items.Count() <= 0)
        return null;

    // FindItem never gets "all" the properties, so now that we've found them all, we need to get them all.
    BaseItemIdType[] itemIds = new BaseItemIdType[items.Count()];
    for (int i = 0; i < items.Count(); i++)
        itemIds[i] = items[i].ItemId;

    GetItemType getItemType = new GetItemType();
    getItemType.ItemIds = itemIds;
    getItemType.ItemShape = new ItemResponseShapeType();
    getItemType.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
    getItemType.ItemShape.BodyType = BodyTypeResponseType.Text;
    getItemType.ItemShape.BodyTypeSpecified = true;

    GetItemResponseType getItemResponse = exchangeServer.GetItem(getItemType);
    ItemType[] messages = new ItemType[getItemResponse.ResponseMessages.Items.Count()];

    for (int j = 0; j < messages.Count(); j++)
        messages[j] = ((ItemInfoResponseMessageType)getItemResponse.ResponseMessages.Items[j]).Items.Items[0];

    return messages;
}

The "messages" variable will be an array of ItemType objects returned from exchange that have all the properties you'd expect for a mail message (Body, Attachments, etc.). I hope this helps!

like image 117
Scott Anderson Avatar answered Sep 20 '22 14:09

Scott Anderson