locked
Retrieve emails from an exchange mailbox using C#? RRS feed

  • Question

  • User993681747 posted

    Hello All,

    I do a simple program to count how many unread emails are present in a mailbox (on a server exchange). My first step and my question are : How can I retrieve emails from an exchange mailbox using C#?

    In all my research, I am finding how to send emails but not retrieve them. 

    I don't want to use third party APIs and would like to keep it as simple as possible...

    Can some one please point me in the right direction?


    Thanks in advance...

    Thursday, February 28, 2008 11:10 AM

Answers

All replies

  • User680737545 posted

    using Outlook = Microsoft.Office.Interop.Outlook; //you need to add the Microsoft Office 11.0 Object Library to use this. mine is version 11 you might have an older or later version.

     

    Outlook.Application outlook = new Outlook.ApplicationClass();
                    Outlook.NameSpace ns = outlook.GetNamespace("Mapi");

                    object _missing = Type.Missing;
                    ns.Logon(_missing, _missing, false, true);


                    Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                   
                    int unread = inbox.UnReadItemCount;

                    foreach (Outlook.MailItem mail in inbox.Items)
                    {
                        string s = mail.Subject;
                    }

    Thursday, February 28, 2008 11:49 PM
  • User993681747 posted

    Thanks Ghinx. That's a good piece of code.

    I guess this library connects Outlook and uses one of my saved profiles to connect the exchange server.

    But I want to access the inbox of any user ( I have the username and password).

    I read somewhere that "If you need to get data from another user's Exchange mailbox, you can use CDO (not officially supported in .NET, but people have been using it) or WebDAV"

    Does someone have a simple piece of code to do that in CDO or/and webdav  and Exchange 2003?

     

     

    Friday, February 29, 2008 6:35 AM
  • User-1936035690 posted
    Hi,
    Can you connect to the exchange server using POP3 or IMAP4 (You can test using OL Express).

    If so, you may want to try using the respective protocol. It will probably be cleaner than using OL.

    Cheers!
    Dave
    Friday, February 29, 2008 6:59 PM
  • User993681747 posted

    "try using the respective protocol"... what do you mean? Let's say I can connect the exchange server using POP3. What API/technology should I use? Do you have a code sample?

    Thx 

    Sunday, March 2, 2008 1:37 PM
  • User-1936035690 posted
    Hi,
    You will either need to implement the POP3 protocol using TCP/IP sockets, or get a POP3 library that does this for you.

    Cheers!
    Dave
    Tuesday, March 4, 2008 6:00 PM
  • User993681747 posted

     I found some good articles to do POP3 with C#:

    http://www.developerfusion.co.uk/show/4071/2/  or  http://www.csharphelp.com/archives2/archive450.html

    You can download some code here, I tried it and it works fine : http://www.codeproject.com/KB/IP/NetPopMimeClient.aspx 


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 5, 2008 4:32 AM
  • User-2096791005 posted

    Hi,

    Could you please let me know how to retrieve emails from mailbox other than default mailbox?

     

    Thanks in advance! 

     

    Tuesday, May 5, 2009 6:44 AM
  • User-389515215 posted

     Hello,,

    Check this out

    Use system.net.sockets;

    I am providing flexibility to enter pop3 id and password from user

    on aspx.

     

    POP3 Server:

    <

    </

    <

    <

    <table><tr><td> <asp:TextBox ID="fld_Server" runat="server" /></td><td>Port: <asp:TextBox ID="fld_Port" runat="server" Text="110" Columns="3" /></td></tr>tr><td>Username: <asp:TextBox ID="fld_Username" runat="server" /></td><td>Password: <asp:TextBox ID="fld_Password" runat="server" TextMode="Password" />td></tr></table>asp:Button ID="btn_Connect" runat="server" Text="Connect" OnClick="btn_Connect_OnClick" /><br />asp:Literal ID="lit_Status" runat="server" />

    and on button click write this

    TcpClient

    tcpClient.Connect(fld_Server.Text,

     

    System.IO.

    lit_Status.Text = strReader.ReadLine() +

      

    WriteBuffer = enc.GetBytes(

    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

    lit_Status.Text += strReader.ReadLine() +

    WriteBuffer = enc.GetBytes(

    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

    lit_Status.Text += strReader.ReadLine() +

    WriteBuffer = enc.GetBytes(

    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

     

     

    {

    ListMessage = strReader.ReadLine();

     

    {

     

    }

     

    tcpClient = new TcpClient();Convert.ToInt32(fld_Port.Text));NetworkStream netStream = tcpClient.GetStream();StreamReader strReader = new System.IO.StreamReader(netStream);"<br />";byte[] WriteBuffer = new byte[1024];ASCIIEncoding enc = new System.Text.ASCIIEncoding();"USER " + fld_Username.Text + "\r\n");"<br />";"PASS " + fld_Password.Text + "\r\n");"<br />";"LIST\r\n");String ListMessage;while (true)if (ListMessage == ".")break;else

    {

    lit_Status.Text += ListMessage +

     

    }

    }

    WriteBuffer = enc.GetBytes(

    netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

    lit_Status.Text += strReader.ReadLine() +

    }

    "<br />";continue;"QUIT\r\n");"<br />";

    Sunday, September 20, 2009 2:25 PM