PowerTCP Mail for .NET | IMAP Emails and Attachments Code Example

See all PowerTCP Mail for .NET Code Examples

IMAP Example

This example demonstrates downloading emails via IMAP, and saving them and their attachments to disk. Also demonstrates SSL/TLS.


using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Net.Security;

Imap imap1 = new Imap();
bool useEncryption = true;

if (useEncryption)
    //Set the method of encryption - Implicit/Explicit
    imap1.Session.Security.Encrypt = Encryption.Explicit;

    //Optionally set the protocols available for SSL/TLS negotiation
    //TLS 1.1/1.2 requires .NET 4.5+ and Windows Vista+. See the SslProtocols MSDN documentation for more information.
    imap1.Session.Security.Protocols = SslProtocols.Tls12;

    //Specify the server certificate validation callback
    imap1.Session.Security.ValidationCallback = remoteCertificateValidation;

//Set the server address and port. If the server uses a non-standard port, it should be substituted here.
//GetDefaultPort() returns the common port used for the security configuration.
imap1.Session.RemoteEndPoint = new Dart.Mail.IPEndPoint("myImapHost.com", Imap.GetDefaultPort(imap1.Session));

//Connect to the server.
//Authenticate the user.
imap1.Session.Username = "myUsername";
imap1.Session.Password = "myPassword";
//Please see the top level help topic "OAuth Overview" in the included help documentation for information on OAuth.
imap1.Session.Authentication = Authentication.Auto;

//Select the inbox and access the messages
foreach (ImapMessage iMessage in imap1.SelectedMailbox.ToArray())
    //Confirm that the message in the copied array wasn't deleted by another client
    if (iMessage.Id != 0)
        //Retrieve each message individually
        //Save the message to disk, or access iMessage.Message.Text, Html or Attachments, etc.
        iMessage.Message.Save(Path.Combine("C:\\MyEmailDirectory\\", message.Uid + ".eml"));

        //Optionally also save all attachments separately
        foreach (Attachment attached in iMessage.Message.Attachments)
            string filePath = Path.Combine("C:\\MyEmailAttachmentsDirectory\\", attached.FileName);
            if (!Attachment.DecodeToMemory)
                //If the component is configured to decode attachments to disk (the default),
                //use the Content property that returns a FileInfo representing the file on disk,
                //and copy the file to the desired location.
                //If the component is configured to decode attachments to memory, get the content
                //MemoryStream with GetContentStream(), and write its content to a file.
                using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))

private bool remoteCertificateValidation(Object sender, X509Certificate remoteCertificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    //For this simple snippet, accept all server certificates. Please see the 'Security' top-level help topic in the help documentation, or
    //the System.Net.Security.RemoteCertificateValidationCallback MSDN documentation, for more information.
    return true;


To download a trial please visit the PowerTCP Mail for .NET product page.