美文网首页
编译MailKit及注意事项

编译MailKit及注意事项

作者: 价值投机168 | 来源:发表于2020-04-29 09:09 被阅读0次

1.去这里下载源代码https://github.com/jstedfast/MailKit
2.注意下载的时候,使用他提供的git方法,不要直接下载,直接下载会缺少代码
3.下载完后,使用vs2017以上的编译,因为他使用了C#7.0的语法
4.还会差两个dll,官方下载地址:
http://www.bouncycastle.org/csharp/index.html
https://github.com/nunit-legacy/nunitv2/releases
5.这样就可以编译出你想要的dll了。

6.pop3获取附件(这个也是官方文档,可以细看):
http://www.mimekit.net/docs/html/P_MimeKit_MimeMessage_Attachments.htm

image.png

7.无论是pop3还是imap都没有一个好的方法只接收新邮件
我们只能拿到邮件列表后自几判断,大多是通过邮件的UID进行判断
注意UID与MessageID并非一个东西,UID是邮箱用来标识你这个账户的每一封邮件的东西,
而MessageID是发送邮件的时候生成的唯一ID,也有可能发送没有你的接收邮箱自己生成,
总是取messageid需要下载邮件的头,这样有联网操作会很慢的,所以我们只需要存储下来uid就好了,
记得保存的时候按照邮箱存储,因为不同的邮箱uid会重复的

8.别人写的,随便参考吧:
https://blog.csdn.net/starfd/article/details/80706227

9.又一个:
https://www.cnblogs.com/peterYong/p/12024394.html

10.这个是发送邮件比较全的了:
https://www.lagou.com/lgeduarticle/50081.html

11.自己写的测试代码:

using MailKit.Net.Smtp;
using MimeKit;
using System.IO;

namespace test2
  {
class Program
{
    static void Main(string[] args)
    {

        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Joey Tribbiani", "123@123.com"));
        message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "123@123.com"));
        message.To.Add(new MailboxAddress("123", "123@123.com"));
        message.Subject = "How you doin'?";

        var plain = new TextPart("plain")
        {
            Text = @"Hey Chandler,

  I just wanted to let you know that Monica and I were going to go       play some paintball, you in?

-- Joey"
        };

        string path = @"C:\Users\21.csv";
        var attachment = new MimePart("text", "csv")
     {
            
             Content = new MimeContent(File.OpenRead(path), ContentEncoding.Default),
            //指定邮件阅读程序处理数据内容的方式,有inline和attachment两种标准方式,inline表示直接处理,而attachment表示当做附件处理。
            ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
            //表示MIME消息体的内容为邮件附件,附件名
            FileName = Path.GetFileName(path)
        };

        path = @"C:\222.csv";
        var attachment2 = new MimePart("text", "csv")
        {

            Content = new MimeContent(File.OpenRead(path), ContentEncoding.Default),
            //指定邮件阅读程序处理数据内容的方式,有inline和attachment两种标准方式,inline表示直接处理,而attachment表示当做附件处理。
            ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
            //表示MIME消息体的内容为邮件附件,附件名
            FileName = Path.GetFileName(path)
        };

        var alternative = new Multipart("alternative");
                    alternative.Add(plain);

        var multipart = new Multipart("mixed");
                   multipart.Add(alternative);
                    multipart.Add(attachment);
        multipart.Add(attachment2);

        message.Body = multipart;
        using (var client = new SmtpClient())
        {
            client.Connect("smtp.office365.com", 587, false);

            // Note: only needed if the SMTP server requires authentication
            client.Authenticate("123@123.com", "123!!");

            client.Send(message);
            client.Disconnect(true);
        }
    }
}
}

12.还有一个讲的不错的:
https://www.cnblogs.com/pengze0902/p/6562447.html

13.Mailkit的IMAP的简单测试程序:

 static void Main(string[] args)
    {
        using (var client = new ImapClient())
        {
            client.SslProtocols = SslProtocols.Default; //加密的
            client.Connect("outlook.office365.com", 993);
            client.Authenticate("用户名,比如aa@163.com", "密码,比如123");

            List<MimeMessage> messages = new List<MimeMessage>();

            var inbox = client.Inbox;
            inbox.Status(StatusItems.Count | StatusItems.Unread);//得先查询下,不然inbox.Unread一直为0
            if (inbox.Unread > 0)//打开比较慢,所以先判断下
            {
                inbox.Open(FolderAccess.ReadOnly);//需要open,才能获取到数据.
                foreach (var uid in inbox.Search(SearchQuery.NotSeen))
                {
                    var message = inbox.GetMessage(uid);
                    messages.Add(message);
                    Console.WriteLine("[match] {0}: {1}", uid, message.Subject);
                } inbox.Close();
            }
           


            IMailFolder trash = null;//找垃圾邮件
            try { trash = client.GetFolder("Trash"); } catch { }
            if (trash == null)
            {
                try { trash = client.GetFolder("TRASH"); } catch { }
                if (trash == null)
                {
                    try { trash = client.GetFolder("垃圾邮件"); } catch { }
                }
            }
            if (trash != null) { trash.Open(FolderAccess.ReadWrite); }//只有读写,才能修改

            foreach (var uid in trash.Search(SearchQuery.NotSeen))
            {
                var message = trash.GetMessage(uid);
                messages.Add(message);
                Console.WriteLine("[trash] {0}: {1}", uid, message.Subject);

                trash.AddFlags(uid, MessageFlags.Seen, true);//修改此条的状态,如果是deleted就是删除了
            }
            if (trash != null) {
                trash.Expunge();
                trash.Close(); }

            Console.WriteLine("****");

            foreach (var me in messages)
            {
                Console.WriteLine(me.Subject);
            }

            IList<IMailFolder> mailFolderList = client.GetFolders(client.PersonalNamespaces[0]);//获取所有的文件夹

            client.Disconnect(true);
        }

        Console.Read();
    }

相关文章

网友评论

      本文标题:编译MailKit及注意事项

      本文链接:https://www.haomeiwen.com/subject/tdgwwhtx.html