Skip to content
Reinaldo Coelho Sartorelli edited this page Jan 23, 2017 · 3 revisions

How to install

From NuGet

    Install-Package MailSystem.Net-trunk

Using with examples

Get emails with POP3

(`using XXXXXX)

Get emails with IMAP4

Simple sync

                private const string _imapLogin = "[login]";
                private const string _imapPassword = "[password]";
                private const int _imapPort = 993;
                private const string _imapServerAddress = "imap.gmail.com";

                var _selectedMailBox = "INBOX";
                using (var _clientImap4 = new Imap4Client())
                {

                    _clientImap4.ConnectSsl(_imapServerAddress, _imapPort);
                    // another option is: _clientImap4.Connect(_mailServer.address, _mailServer.port);

                    _clientImap4.Login(_imapLogin, _imapPassword); // Make log in and load all MailBox.
                    //_clientImap4.LoginFast(_imapLogin, _imapPassword); // Only make login.

                    var _mailBox = _clientImap4.SelectMailbox(_selectedMailBox);

                    foreach (var messageId in _mailBox.Search("ALL").AsEnumerable())
                    {
                        var message = _mailBox.Fetch.Message(messageId);
                        var _imapMessage = Parser.ParseMessage(message);
                    }

                    _clientImap4.Disconnect();
                }

Simple Async

        private const string _imapLogin = "[login]";
        private const string _imapPassword = "[password]";
        private const int _imapPort = 993;
        private const string _imapServerAddress = "imap.gmail.com";

        public void DownloadImapAsync()
        {
            try
            {
                var callBack = new AsyncCallback(ImapAsyncCallBack);

                using (var _clientImap4 = new Imap4Client())
                {

                    var result = _clientImap4.BeginConnectSsl(_imapServerAddress, _imapPort, callBack);
                    
                    while(!result.CompletedSynchronously)
                    {
                        Console.WriteLine("Wait async finish....");
                    }
                }
                
                Console.WriteLine("End process....");
            }
            catch (Exception e)
            {
                Console.WriteLine("Dont work");
            }
        }

        private void ImapAsyncCallBack(IAsyncResult result)
        {
            var resultStatus = result.AsyncState;
            using (var _clientImap4 = (Imap4Client)((System.Delegate)result.AsyncState).Target)
            {
                _clientImap4.Login(_imapLogin, _imapPassword); // Make log in and load all MailBox.
                //_clientImap4.LoginFast(_imapLogin, _imapPassword); // Only make login.

                var _selectedMailBox = "INBOX";
                var _mailBox = _clientImap4.SelectMailbox(_selectedMailBox);

                foreach (var messageId in _mailBox.Search("ALL").AsEnumerable())
                {
                    var message = _mailBox.Fetch.Message(messageId);
                    var _imapMessage = Parser.ParseMessage(message);
                }

                _clientImap4.Disconnect();
            }
        }
Clone this wiki locally