[ACCEPTED]-Access exchange e-mail in C#-exchange-server

Accepted answer
Score: 18

If you use Exchange 2007 and have web services 8 enabled, this is pretty easy. I added a 7 2.0-style classic Web Reference to my VS2008 6 project, and I can get mail messages like 5 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 4 array of ItemType objects returned from 3 exchange that have all the properties you'd 2 expect for a mail message (Body, Attachments, etc.). I 1 hope this helps!

Score: 6

Depends on the Exchange version. WebDAV works 5 with 2000 thru 2007, but Web Services requires 2007+.

Those 4 are probably the easiest to get working. CDO 3 is another option, but it's not supported from C# - so you'll have 2 to go out of proc.

Exchange also has an OLEDB provider, but I've 1 never used it - it is supported from .NET, however.

Score: 5

Using EWS directly in managed code (VB.net 14 / C#) is clumsy to say the best.

I've been 13 fumbling around with it for a few days now, and 12 have come to the conclusion that it's better 11 to build my own wrapper classes around the 10 API, making the services usable in a line 9 or two of code, not the page with the current 8 implementation.

Guess what? Microsoft have 7 beaten me to it: Exchange Web Services Managed 6 API's first Release Candidate is available 5 for download here.

Install, register the dll 4 reference (\Program Files\Microsoft\Exchange\Web 3 Services\1.0\Micorosft.Exchange.WebServices.dll), and 2 import the namespace (Microsoft.Exchange.WebServices.Data) and 1 you're ready to roll.

Score: 3

I assume your issue is that your exchange 29 server only support NTLM authentication 28 and does not allow plain text authentication? Or 27 you might not be using the proper username 26 convention. For example you might try using 25 the format username@domain where domain 24 is the internal NT domain which might not 23 be the same as your internet domain.

If 22 that is the case then look for a library 21 that supports NTLM.

Steps for testing via 20 telnet

Go to command prompt type: telnet 19 my.server.com 110 you should get a response 18 from your exchange server like this +OK 17 Microsoft Exchange Server 2003 POP3 server 16 version 6.5.7638.1 (my.server.com) ready.

type: CAPA this 15 should return the list of capabilities your 14 exchange server supports. CAPA +OK Capability 13 list follows TOP USER PIPELINING EXPIRE 12 NEVER UIDL SASL NTLM .

Notice that mine does 11 not show PLAIN

Here is a response from an 10 email server that does+OK Dovecot ready. CAPA +OK CAPA TOP UIDL RESP-CODES PIPELINING STLS USER SASL 9 PLAIN .

If your response does not include 8 PLAIN then stop as you need a library that 7 supports SPA

type: user myusername OR type: user 6 myusername@domain.corp replacing domain.corp 5 with your domain

You should then receive +OK

Type: pass 4 mypass

You should get a response +OK

type: list 3

Should get a list of emails. This might 2 help see if your issue is a username format 1 issue.

Score: 2

I wrote this to get email from my inbox. I 9 have a tedious task to do every morning 8 if an email is there, so I wrote this bit 7 of code to check my folder for an email 6 title. I added a small bit of xml creation 5 to show the possibilities. Its not exchange 4 but it works in my case. XCData encodes 3 any special char in the body. I guess I 2 should point out that email subject I'm 1 looking for is [Ticket - Support#12345]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office;
using System.Xml.Linq;

namespace ProcessEmail
    class Program
        static void Main(string[] args)
            Outlook.Application outlook = new Outlook.Application();
            Outlook.NameSpace ns = outlook.GetNamespace("Mapi");
            object _missing = Type.Missing;
            ns.Logon(_missing, _missing, false, true);

            Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

            int unread = inbox.UnReadItemCount;
            XElement xmlMail = new XElement("Mail");
            foreach (Outlook.MailItem mail in inbox.Items)
                string s = mail.Subject;

                if (s != null)
                    if (s.Contains("Tickets") || (s.Contains("Support")))
                        string[] splitter = s.Split('#');
                        string[] split = splitter[1].Split(']');                       
                        string num = split[0].Trim();

                        XElement mailrow = new XElement("MailRow",
                            new XElement("Ticket_Number",num),
                            new XElement("Subject", mail.Subject),
                            new XElement("Body",  new XCData(mail.Body)),
                            new XElement("From", mail.SenderEmailAddress)




Score: 1

You need to use the Exchange SDK if POP3 is not enabled 2 in the Exchange Server. Another options 1 is to use WebDAV.

Score: 1

You could use this library: http://www.dimastr.com/redemption/


Score: 1

Another option is to configure Exchange 2 to enable IMAP4. There exist 3rd party 1 IMAP4 libraries for .NET, e.g. rebex.

Score: 1

You could use EWS (Exchange webservices) starting 7 from Exchange 2007. They seem to be always 6 installed and are better supported than 5 Webdav.

You can just import the webservice 4 from your Exchangeserver (Search for an 3 asmx-file in the installation directory). You 2 can download EML-files from your mails and 1 do many more things!

Score: 1

You can use Exchange Web Services (Exchange 2007 or 2010) or 9 WebDav (Exchange up to 2007, 2010 does not support 8 WebDAV), but the API and implementation 7 might be a bit cumbersome if you want to 6 do quick development.

I have used IndependentSoft's 5 libraries for WebDav and Exchange Web Services successfully in the 4 past, which provide a wrapper around the 3 Exchange APIs and are much simpler to use. They 2 handle parsing the message and MIME quite 1 well too.

Score: 0
  1. The currently preferred API (in Exchange 32 2013 and 2016) is EWS. It is purely HTTP based 31 and can be accessed from any language, but 30 there are .Net and Java specific libraries.

    You can 29 use EWSEditor to play with the API.

  2. Extended MAPI. This is the 28 native API used by Outlook. It ends up using 27 the MSEMS Exchange MAPI provider, which 26 can talk to Exchange using RPC (Exchange 25 2013 no longer supports it) or RPC-over-HTTP 24 (Exchange 2007 or newer) or MAPI-over-HTTP 23 (Exchange 2013 and newer).

    The API itself 22 can only be accessed from unmanaged C++ or 21 Delphi. You can also use Redemption (any language) - its 20 RDO family of objects is an Extended MAPI wrapper. To 19 use Extended MAPI, you need to install either 18 Outlook or the standalone (Exchange) version of MAPI (on extended support, and 17 it does not support Unicode PST and MSG 16 files and cannot access Exchange 2016). Extended 15 MAPI can be used in a service.

    You can play 14 with the API using OutlookSpy or MFCMAPI.

  3. Outlook Object Model - not Exchange 13 specific, but it allows access to all data 12 available in Outlook on the machine where 11 the code runs. Cannot be used in a service.

  4. Exchange Active Sync. Microsoft 10 no longer invests any significant resources 9 into this protocol.

  5. Outlook used to install 8 CDO 1.21 library (it wraps Extended MAPI), but 7 it had been deprecated by Microsoft and 6 no longer receives any updates.

  6. There used 5 to be a third-party .Net MAPI wrapper called 4 MAPI33, but it is no longer being developed 3 or supported.

  7. WebDAV - deprecated.

  8. Collaborative 2 Data Objects for Exchange (CDOEX) - deprecated.

  9. Exchange 1 OLE DB Provider (EXOLEDB) - deprecated.

More Related questions