using c# .net librarires to check for IMAP message

2019-01-03 05:45发布

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?

标签: c# .net email imap
10条回答
欢心
2楼-- · 2019-01-03 06:27

MailSystem.NET contains all your need for IMAP4. It's free & open source.

(I'm involved in the project)

查看更多
倾城 Initia
3楼-- · 2019-01-03 06:31

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.Search(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.Text);
    }
    imap.Close(true);
}

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

You can download it here: https://www.limilabs.com/mail.

查看更多
家丑人穷心不美
4楼-- · 2019-01-03 06:36

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);

    }
}
查看更多
闹够了就滚
5楼-- · 2019-01-03 06:38

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

查看更多
再贱就再见
6楼-- · 2019-01-03 06:40

I'd recommend looking at MailKit as it is probably the most robust mail library out there and it's Open Source (MIT).

One of the awesome things about MailKit is that all network APIs are cancelable (something I haven't seen available in any other IMAP library).

It's also the only library that I know of that supports threading of messages.

using System;
using System.Net;
using System.Threading;

using MailKit.Net.Imap;
using MailKit.Search;
using MailKit;
using MimeKit;

namespace TestClient {
    class Program
    {
        public static void Main (string[] args)
        {
            using (var client = new ImapClient ()) {
                using (var cancel = new CancellationTokenSource ()) {
                    client.Connect ("imap.gmail.com", 993, true, cancel.Token);

                    // If you want to disable an authentication mechanism,
                    // you can do so by removing the mechanism like this:
                    client.AuthenticationMechanisms.Remove ("XOAUTH");

                    client.Authenticate ("joey", "password", cancel.Token);

                    // The Inbox folder is always available...
                    var inbox = client.Inbox;
                    inbox.Open (FolderAccess.ReadOnly, cancel.Token);

                    Console.WriteLine ("Total messages: {0}", inbox.Count);
                    Console.WriteLine ("Recent messages: {0}", inbox.Recent);

                    // download each message based on the message index
                    for (int i = 0; i < inbox.Count; i++) {
                        var message = inbox.GetMessage (i, cancel.Token);
                        Console.WriteLine ("Subject: {0}", message.Subject);
                    }

                    // let's try searching for some messages...
                    var query = SearchQuery.DeliveredAfter (DateTime.Parse ("2013-01-12"))
                        .And (SearchQuery.SubjectContains ("MailKit"))
                        .And (SearchQuery.Seen);

                    foreach (var uid in inbox.Search (query, cancel.Token)) {
                        var message = inbox.GetMessage (uid, cancel.Token);
                        Console.WriteLine ("[match] {0}: {1}", uid, message.Subject);
                    }

                    client.Disconnect (true, cancel.Token);
                }
            }
        }
    }
}
查看更多
登录 后发表回答