Here is a working example of IMAP4 with Gmail

Aug 14, 2011 at 9:32 PM
Edited Aug 14, 2011 at 9:33 PM

I needed an IMAP solution to interact with GMail and tried several of the open options that are available. None was as easy to implement, as stable, or as simple to work with as MailSystem.NET.

The biggest problem I've had is trying to dig through this discussion group looking at code with errors to try and figure out how to implement it, since there is no documentation, or even simple examples that work. I read someone's post asking for an example, and since I would have been very greatful if someone would have just posted a little code to show how their solution works, here's my working code.

This just grabs all unread (UNSEEN) messages off the server and writes them to an XML file. I'm not a formally educated programmer, so go easy on any criticism, thanks.

Added references to the DLLs: ActiveUp.Net.Common, ActiveUp.Net.Mail, ActiveUp.Net.Imap4

              using ActiveUp.Net.Mail; 

 

 

 

            Imap4Client imap = new Imap4Client();
            imap.ConnectSsl("imap.gmail.com", 993);
            imap.Login("heythatsme@gmail.com", "heythatsmypassword");
            imap.Command("capability");

            Mailbox inbox = imap.SelectMailbox("inbox");
            int[] ids = inbox.Search("UNSEEN");
            if (ids.Length > 0)
            {
                Message msg_first = inbox.Fetch.MessageObject(ids[0]);
                
                //ignore this gmail_data stuff // undefined in this scope // checking to make sure it's a "new" unread msg
                if (gmail_data != msg_first.Date.ToString())
                {
                    gmail_data = msg_first.Date.ToString();

                    XElement xmail = new XElement("gmail",
                        new XAttribute("count", ids.Length.ToString()),
                        new XAttribute("modified", msg_first.Date.ToString())
                    );

                    string name = "", address = "", from = "";
                    Regex reg_name = new Regex("\"[^\"]+");
                    Regex reg_address = new Regex("<[^>]+");

                    Message msg = null;

                    for (var i = 0; i < ids.Length; i++)
                    {
                        msg = inbox.Fetch.MessageObject(ids[i]);

                        from = msg.HeaderFields["from"];
                        name = reg_name.Match(from).Value.Replace("\"", "");
                        address = reg_address.Match(from).Value.Replace("<", "");

                        xmail.Add(new XElement("entry",
                            new XAttribute("id", msg.MessageId),
                            new XAttribute("modified", msg.Date.ToString()),
                            new XAttribute("name", name),
                            new XAttribute("address", address),
                            new XElement("subject", msg.Subject),
                            new XElement("body-text", msg.BodyText.TextStripped),
                            new XElement("body-html", msg.BodyHtml.Text)
                        ));
                    }

                    
                    File.WriteAllText(@"E:\srv\pub\http\interop\feeds\gmail.xml", xmail.ToString());
                    
                    
                }
            }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Jun 17, 2013 at 5:31 AM
Thank you it worked !!!
Thanks.
Jul 23, 2013 at 3:41 PM
I just tried to compile this example using SharpDevelop C# 5.0 and .NET 4.0 and received this error:

The referenced assembly "ActiveUp.Net.Imap4" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project. (MSB3253)

Any ideas?
Jul 24, 2013 at 3:01 PM
ClayGoss: Try setting your project Target Framework to .NET Framework 4 instead of .NET Framework 4 Client Profile. I had the same issue, but changing this resolved it.

thomastmc: Excellent work, you saved me many hours of googling
Aug 29, 2013 at 7:43 AM
Thank you, fixed issue with Target Platform.
Still trying to figure out how to read all messages by Search query.
Nov 11, 2013 at 12:09 PM
Hi i am trying to connect to my local outlook and access messages from it. can u please let me know how the connection should be written..
Dec 18, 2013 at 5:28 PM
@shiba: Don't hijack threads that refer to something completely different. No-one will find your question, let alone answer it.

This library connects to email servers, Outlook is client software, so you won't succeed using this library.
Nov 28, 2014 at 10:38 AM
Hie Guys I am getting Errors at


XElement XAttribute Xmail Regex reg_name = new Regex("\"[^\"]+");
                                                             Regex reg_address = new Regex("<[^>]+")             
I added all the references tooo
Jul 30, 2015 at 7:33 AM
Hey,

i'm trying to do this, and on the
imap.ConnectSsl("imap.gmail.com", 993);
line i get an error after a few seconds:



{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.133.108:993"}


i can connect to my gmail from the browser, and i have ping to that (74.125.133.108) address, and i've allowed the imap in my gmail settings..

i also tried to run the app ass admin..


anyone knows what may be the problem?...