tags:

views:

7606

answers:

7

Hi,

Does anyone have any sample code in that makes use of the .Net framework that connects to googlemail servers via IMAP SSL to check for new emails?

Thanks

+7  A: 

The URL listed here might be of interest to you

http://www.codeplex.com/InterIMAP

which was extension to

http://www.codeproject.com/KB/IP/imaplibrary.aspx?fid=91819&df=90&mpp=25&noise=5&sort=Position&view=Quick&fr=26&select=2562067#xx2562067xx

lakshmanaraj
this project doesnt handle ssl connections but there is a comment from the author that asks you to send him an email to request the gmail ssl code, thanks
Belliez
InterIMAP isn't brilliant. It really freaks out when you connect to a large mailbox. (As it insists on reading every single header before it will allow you to do anything).
James
There is a new asynchronous version of the library that addresses many of the speed issues from the previous version.
Jason Miesionczek
+1  A: 

the source to the ssl version of this is here: http://atmospherian.wordpress.com/downloads/

Belliez
+9  A: 

As the author of the above project i can say that yes it does support SSL. I am currently working on a new version of the library that will be completely asynchronous to increase the speed with which it can interact with IMAP servers. That code, while not complete, can be downloaded, along with the original synchronous library (which also supports SSL), from the code plex site linked to above.

thanks for the reference lakshmanaraj :)

Jason Miesionczek
Thank you, nice job!
Dänu
+5  A: 

Lumisoft.net has both IMAP client and server code that you can use.

I've used it to download email from Gmail. The object model isn't the best, but it is workable, and seems to be rather flexible and stable.

Here is the partial result of my spike to use it. It fetches the first 10 headers with envelopes, and then fetches the full message:

using (var client = new IMAP_Client())
{
    client.Connect(_hostname, _port, _useSsl);
    client.Authenticate(_username, _password);
    client.SelectFolder("INBOX");
     var sequence = new IMAP_SequenceSet();
    sequence.Parse("0:10");
    var fetchItems = client.FetchMessages(sequence, IMAP_FetchItem_Flags.Envelope | IMAP_FetchItlags.UID,
                                        false, true);
    foreach (var fetchItem in fetchItems)
    {
        Console.Out.WriteLine("message.UID = {0}", fetchItem.UID);
        Console.Out.WriteLine("message.Envelope.From = {0}", fetchItem.Envelope.From);
        Console.Out.WriteLine("message.Envelope.To = {0}", fetchItem.Envelope.To);
        Console.Out.WriteLine("message.Envelope.Subject = {0}", fetchItem.Envelope.Subject);
        Console.Out.WriteLine("message.Envelope.MessageID = {0}", fetchItem.Envelope.MessageID);
    }
    Console.Out.WriteLine("Fetching bodies");
    foreach (var fetchItem in client.FetchMessages(sequence, IMAP_FetchItem_Flags.All, false, true)
    {             
        var email = LumiSoft.Net.Mail.Mail_Message.ParseFromByte(fetchItem.MessageData);             
        Console.Out.WriteLine("email.BodyText = {0}", email.BodyText);

    }
}
Bruno Lopes
It appears he has changed his syntax. Instead of calling Authenticate you now call client.Login(_username, _password); And the FetchMessages has been replaced with a Fetch that works very differently. The Lumisoft code is fantastic though, but could use more samples of how to use it.
Jason Short
Thanks! LumiSoft's implementation works wonders!
Cetin Sert
A: 

I Used Lumisoft coding. From,to fields are rereived in mailbox format. Can anybody tell me how to convert mailbox to string. It's just displaying as "LumiSoft.Net.Mail.Mail_t_Mailbox[]". How to convert this to string??

Pls help

+2  A: 

@Shalini - that's because From and To are arrays of FetchItems that have more information inside (e.g. Display Name, domain, etc.), so to get just all the From email addresses, do something like:

foreach (var fAddress In fetchItem.Envelope.From)
{
      Console.Out.WriteLine("message.Envelope.From = {0}", fAddress.Address);
}
A: 

There is no .NET framework support for IMAP. You'll need to use some 3rd party component.

Try Mail.dll email component, it's very affordable and easy to use, it also supports SSL:

using(Imap imap = new Imap())
{
    imap.ConnectSSL("imap.company.com");
    imap.Login("user", "password");

    imap.SelectInbox();
    List<long> uids = imap.SearchFlag(Flag.Unseen);
    foreach (long uid in uids)
    {
        string eml = imap.GetMessageByUID(uid);
        IMail message = new MailBuilder()
            .CreateFromEml(eml);

        Console.WriteLine(message.Subject);
        Console.WriteLine(message.TextDataString);
    }
    imap.Close(true);
}

Please note that this is a commercial product I've created.

You can download it here: http://www.lesnikowski.com/mail.

Pawel Lesnikowski