Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C# 4.0

PopClient - A POP3 companion to SmtpClient

Rate me:
Please Sign up or sign in to vote.
4.94/5 (55 votes)
19 Nov 2010CPOL16 min read 223.3K   4.9K   111  
PopClient is an asynchronous POP3 library with support for SSL and attachments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.IO;

namespace Extra.Mail
{
    /// <summary>
    /// This class is used to convert a CDO message instance to a MailMessage object
    /// as well as exposing properties not present in MailMessage.
    /// </summary>
    internal class CDOMessageConverter
    {
        private CDO.Message cdoMessage;

        public CDOMessageConverter(string mailContent)
        {
            cdoMessage = new CDO.Message();
            ADODB.Stream adoStream = cdoMessage.GetStream();
            adoStream.Type = ADODB.StreamTypeEnum.adTypeText;
            adoStream.WriteText(mailContent.Trim(), ADODB.StreamWriteEnum.adWriteLine);
            adoStream.Flush();
            adoStream.Close();
        }

        public DateTime ReceivedTime
        {
            get
            {
                return cdoMessage.ReceivedTime;
            }
        }

        public MailMessage ToMailMessage()
        {
            MailMessage message = new MailMessage();

            if (!String.IsNullOrWhiteSpace(cdoMessage.From))
            {
                message.From = new MailAddress(cdoMessage.From);
            }

            if (!String.IsNullOrWhiteSpace(cdoMessage.Sender))
            {
                message.Sender = new MailAddress(cdoMessage.Sender);
            }

            message.Subject = cdoMessage.Subject;

            if (!String.IsNullOrWhiteSpace(cdoMessage.To))
            {
                message.To.Add(cdoMessage.To);
            }

            if (!String.IsNullOrWhiteSpace(cdoMessage.CC))
            {
                message.CC.Add(cdoMessage.CC);
            }

            if (!String.IsNullOrWhiteSpace(cdoMessage.BCC))
            {
                message.Bcc.Add(cdoMessage.BCC);
            }

            if (String.IsNullOrWhiteSpace(cdoMessage.HTMLBody))
            {
                message.Body = cdoMessage.TextBody;
            }
            else
            {
                message.Body = cdoMessage.HTMLBody;
                message.IsBodyHtml = true;
            }

            if (!String.IsNullOrWhiteSpace(cdoMessage.ReplyTo))
            {
                message.ReplyToList.Add(cdoMessage.ReplyTo);
            }

            foreach (CDO.IBodyPart bodyPart in cdoMessage.Attachments)
            {
                ADODB.Stream stream = bodyPart.GetDecodedContentStream();
                IStream comStream = stream as IStream;
                byte[] bytes = new byte[stream.Size];
                ulong bytesRead = 0;
                var handle = GCHandle.Alloc(bytesRead, GCHandleType.Pinned);
                IntPtr pcbRead = handle.AddrOfPinnedObject();
                comStream.Read(bytes, stream.Size, pcbRead);
                handle.Free();
                using (MemoryStream memoryStream = new MemoryStream(bytes))
                {
                    message.Attachments.Add(new Attachment(memoryStream, bodyPart.FileName));
                }
            }

            foreach (ADODB.Field field in cdoMessage.Fields)
            {
                if (field.Type == ADODB.DataTypeEnum.adBSTR || field.Type == ADODB.DataTypeEnum.adDate)
                {
                    string key = field.Name.Split(':').Last();
                    string value = field.Value.ToString();
                    if (key != null && value != null)
                    {
                        message.Headers.Add(key, value);
                    }
                }

            }

            return message;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions