Click here to Skip to main content
15,895,084 members

read mails from exchange 2010 using c#

rambuzz asked:

Open original thread
Hi All,

Please find the code that i have written below for connecting to exchange server 2010 and reading the mails via IMAp. it is working fine for exchange 2007 but failing for exchange 2010. kindly help me on the same :
C#
  private void button1_Click(object sender, EventArgs e)
        {
            
            ReadIMAPEmail();
        }
        
        private void ReadIMAPEmail()
        {
            string Mailserver = ConfigurationSettings.AppSettings["MailServer"];
            string MailServerPort = ConfigurationSettings.AppSettings["MailServerPort"];
            string MailServerUsername = ConfigurationSettings.AppSettings["MailServerUsername"];
            string MailServerPwd = ConfigurationSettings.AppSettings["MailServerPwd"];
            ImapClient imap = new ImapClient(Mailserver, Convert.ToInt32(MailServerPort), true);
            imap.IsDebug = true;
            try
            {
                WriteEventLogEntry("Checking for Server Authentication....");
                if (imap.Connection())
//Iam getting the error message here after trying to connect to the server on particar port.But when i verified using telnet it is opening on that port

                {
                    WriteLog("Loggin in to IMAP server.", Color.Green);
                    
                    bool Islogin = imap.LogIn(MailServerUsername, MailServerPwd);
                    if (Islogin)
                    {
                        
                        WriteLog("Connected to IMAP server.", Color.Green);

                        ImapX.MessageCollection messages = imap.Folders["INBOX"].Search("ALL", true);

                        foreach (ImapX.Message m in imap.Folders["INBOX"].Messages)
                        {
                            try
                            {
                                m.Process();
                                List<imapx.mailaddress> ListTo = new List<imapx.mailaddress>();
                                List<imapx.mailaddress> ListFrom = new List<imapx.mailaddress>();
                                string textBody = m.TextBody.TextData;

                                string htmlBody = m.HtmlBody.TextData;
                                string strfromaddress = string.Empty;
                                string strfromname = string.Empty;

                                string strsubject = m.Subject.ToString();
                                string strto = string.Empty;
                                string strtoname = string.Empty;
                                ListTo = m.To;
                                ListFrom = m.From;
                                for (int i = 0; i < ListTo.Count; i++)
                                {
                                    strto = strto + ListTo[i].Address;
                                    strtoname = strtoname + ListTo[i].DisplayName;
                                }
                                for (int i = 0; i < ListFrom.Count; i++)
                                {
                                    strfromaddress = strfromaddress + ListFrom[i].Address;
                                    strfromname = strfromname + ListFrom[i].DisplayName;
                                }
                               //delete message
                                WriteLog("Deleting mail " + m.MessageId);
                                m.SetFlag(ImapFlags.DELETED);

                            }
                            catch (Exception ex)
                            {
                                
                                WriteLog("Error while reading email. " + ex.Message, Color.Red);
                            }
                        }
                       
                        imap.LogOut();
                    }
                    else
                    {
                        
                        WriteLog("Cannot Login to IMAP Server. Please check your account details.", Color.Red);
                    }
                }
                WriteLog("Finished checking IMAP mailbox....");
            }
            catch (Exception ex)
            {
               
                WriteLog("Error while connecting to IMAP server. " + ex.Message, Color.Red);

            }
        }
        
        private void WriteLog(string str)
        {
            WriteLog(str, Color.Black);
        }

        private void WriteLog(string str, Color mycolor)
        {
            lblStatus.Text = str;
            lblStatus.ForeColor = mycolor;
        }
        }
Tags: C# (C# 4.0)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900