码迷,mamicode.com
首页 > Web开发 > 详细

使用OpenPop.Net收取邮件

时间:2020-07-03 17:43:05      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:net   byte   str   version   exce   nec   nts   this   using   

 Console.WriteLine("OpenPop.Pop3.Pop3Client:");
            try
            {

                using (var client = new OpenPop.Pop3.Pop3Client())
                {
                    client.Connect(host, port, false);
                    client.Authenticate(address, password);
                    var count = client.GetMessageCount();
                    for (int i = count; i > 0; i--)
                    {
                        OpenPop.Mime.Message message = client.GetMessage(i);
                        Console.WriteLine("Subject:" + message.Headers.Subject);
                        var messagePart = message.MessagePart;
                        string body = " ";
                        if (messagePart.IsText)
                        {
                            body = messagePart.GetBodyAsText();
                        }
                        else if (messagePart.IsMultiPart)
                        {
                            var plainTextPart = message.FindFirstPlainTextVersion();
                            if (plainTextPart != null)
                            {

                                body = plainTextPart.GetBodyAsText();
                            }
                            else
                            {

                                List<MessagePart> textVersions = message.FindAllTextVersions();
                                if (textVersions.Count >= 1)
                                    body = textVersions[0].GetBodyAsText();
                                else
                                    body = "<<OpenPop>> Cannot find a text version body in this message.";
                            }
                            Console.WriteLine("body:" + body);

                            //遍历并获取邮件附件
                            foreach (MessagePart attachment in message.FindAllAttachments())
                            {
                                Console.WriteLine("FileName:" + attachment.FileName);
                                //File.WriteAllBytes(attachment.FileName, attachment.Body);
                            }

                        }


                    }
                 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

Nuget引用OpenPop.Net到项目中

OpenPop.Pop3.Pop3Client对象连接服务器

支持POP3\IMAP协议,

host, port保证正确就能连接,
address, password保证正确就能登录

使用OpenPop.Net收取邮件

标签:net   byte   str   version   exce   nec   nts   this   using   

原文地址:https://www.cnblogs.com/lizhenhong/p/13231168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!