Tuesday, 13 December 2016

Read email of outlook of pop3 server

when you read email if your server is pop3 server.


using (Pop3Client client = new Pop3Client())
                {

                    // Connect to the server
                    client.Connect(EmailServer, Convert.ToInt32(Port), true);

                    // Authenticate ourselves towards the server
                    client.Authenticate(UserName, Password);

                    // Get the number of messages in the inbox
                    int messageCount = client.GetMessageCount();

                    List<Email> Emails = new List<Email>();
                    int counter = 0;
                    for (int i = messageCount; i >= 1; i--)
                    {
                        DateTime currentDate = DateTime.Now.AddDays(-2).Date;

                        Message message = client.GetMessage(i);
                        Email email = new Email()
                        {
                            MessageNumber = i,
                            Subject = message.Headers.Subject,
                            DateSent = message.Headers.Date,
                            From = message.Headers.From.Address,
                        };

                     
                            MessagePart body = message.FindFirstHtmlVersion();
                            if (body != null)
                            {
                                email.Body = body.GetBodyAsText();
                            }
                            else
                            {
                                body = message.FindFirstPlainTextVersion();
                                if (body != null)
                                {
                                    email.Body = body.GetBodyAsText();
                                }
                            }
                            List<MessagePart> attachments = message.FindAllAttachments();

                            foreach (MessagePart attachment in attachments)
                            {
                                email.Attachments.Add(new Attachment
                                {
                                    FileName = attachment.FileName,
                                    ContentType = attachment.ContentType.MediaType,
                                    Content = attachment.Body
                                });
                                string filePath = mailbox + "\\" + attachment.FileName;
                                if (Path.GetExtension(filePath) == ".xlsx" || Path.GetExtension(filePath) == ".xls")
                                {
                                    FileStream Stream = new FileStream(filePath, FileMode.Create);
                                    Stream.Position = 0;
                                    BinaryWriter BinaryStream = new BinaryWriter(Stream);
                                    BinaryStream.Write(attachment.Body);
                                    Stream.Close();
                                    BinaryStream.Close();
                                }
                            }


                            Emails.Add(email);
                        }
                        counter++;
                        if (currentDate >= Convert.ToDateTime(email.DateSent).Date)
                        {
                            break;
                        }
                 

No comments:

Post a Comment