Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IMAP MailClient C#

0.00/5 (No votes)
10 Jan 2011 1  
This is the complete IMAP MailClient using C#

Introduction

What the article/code snippet does, why it's useful, the problem it solves etc.

Background

(Optional) Is there any background to this article that may be useful such as an introduction to the basic ideas presented?

Using the code

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

Blocks of code should be set as style "Formatted" like this:

     //-->This is delegate for the thread
	 private delegate void IUThread();
	//-->This is my forms Click event.It will allow to do in a UIThread.
        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            Thread objThread = new Thread(new ThreadStart(DownloadMail));
            objThread.Start();
        }
    
        private void DownloadMail()
        {
            DataTable dtRows = new DataTable();
            dtRows.Columns.Add("FromId", typeof(string));
            dtRows.Columns.Add("Date", typeof(string));
            dtRows.Columns.Add("Subject", typeof(string));
            dtRows.Columns.Add("No", typeof(string));
            //--> This use for gmail.
            #region Gmail
            //string host = "imap.gmail.com";
            //string username = "userName@gmail.com";
            //string password = "";
            #endregion            
            //--> This is for yahoo.
            #region Yahoo
            string host = "imap.mail.yahoo.com";
            string username = "">userName@yahoo.com";
            string password = "";
            #endregion
            
            #region ImapExample
            TcpIMAP imap = new TcpIMAP();
            imap.Connect(host, 993);
            //--> This is Gmail User Authentication.
            #region Gmail Authentication User
            //imap.AuthenticateGmailUser(username, password);
            #endregion
            //--> This is Yahoo User Authentication.
            #region Yahoo Authentication User
            imap.AuthenticateYahooUser(username, password);
            #endregion
            
            mailCount = imap.MailCount();
            imap.MailUnreadCount();
            // You need to select the inbox in order to view the your messages
            imap.SelectInbox();
            string sSubjectToTrap = "";
            string sFromIdToTrap = "";
            string sDateTime = "";
            /*
             * Uncomment code below to get the message header and body for the 
             * first message in your mailbox.
             * */
            try
            {
                for (int i = 1; i <= mailCount; i++)
                {
                    dump = (string)imap.GetMessageHeaders(i);
                    if (dump.Contains("Subject"))
                    {
                        sSubjectToTrap = hlprCapStr(dump, "Subject:").Trim();
                        sFromIdToTrap = hlprCapStr(dump, "From:").Trim();
                        sFromIdToTrap = sFromIdToTrap.Substring(sFromIdToTrap.IndexOf("<") + 1);
                        sFromIdToTrap = sFromIdToTrap.Replace(">", "");
                        sDateTime = hlprCapStr(dump, "Date:").Trim();
                        this.Dispatcher.BeginInvoke((IUThread)delegate
                        {
                            dtRows.Rows.Add(sSubjectToTrap, sDateTime, sFromIdToTrap, i);
                            dtMail.ItemsSource = dtRows.DefaultView;
                            progressMail.Value = System.Convert.ToDouble(i) / System.Convert.ToDouble(mailCount) * 100;
                        }
                        );
                    }
                }
            }
            catch(Exception)
            {
            }
            finally
            {
                MessageBox.Show("Congratulation you done...!!" + "  " + mailCount + "  Mail");
                imap.Disconnect();
            }
            #endregion   
        }

Remember to set the Language of your code snippet using the Language dropdown.

Use the "var" button to to wrap Variable or class names in &lt;code&gt; tags like this.

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

Keep a running update of any changes or improvements you've made here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here