使用OpenPop.Net收取邮件

 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保证正确就能登录
posted on 2020-07-03 16:30  走出高度  阅读(510)  评论(0编辑  收藏  举报