Click here to Skip to main content
13,056,229 members (107,741 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 :
  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;
        }
        }
Posted 4-Jul-12 0:53am
rambuzz657
Updated 4-Jul-12 1:03am
v3
Comments
Björn Ranft 4-Jul-12 14:45pm
   
What exception is thrown with Exchange 2010? Regards
Member 10325342 19-Oct-13 4:58am
   
please send imapclient library for that code
rambuzz 4-Jul-12 22:31pm
   
Hi,
The error iam getting is "cannot connect to the server."
Thanks,
Ram
Bernhard Hiller 5-Jul-12 6:18am
   
What's that "ImapClient"? Is that a Third Party component?
rambuzz 5-Jul-12 6:50am
   
Yes Bernhard,
it is an IMAPX dll component that is used to read the mails from exchange. For more info u can refer the below url :
http://www.fryan0911.com/2011/11/c-read-emails-using-imap.html
Thanks,
Ram

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170728.5 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100