使用OpenPop.Net收取邮件

2021-01-26 08:15

阅读:609

标签: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 textVersions = message.FindAllTextVersions();
                                if (textVersions.Count >= 1)
                                    body = textVersions[0].GetBodyAsText();
                                else
                                    body = "> 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


评论


亲,登录后才可以留言!