GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Reply to Email in C# and VB.NET

The following example shows how to search for a specific message in the INBOX folder using ImapClient. A reply email message is created in C# and VB.NET with the required reply headers, 'In-Reply-To' and 'References', and a text and HTML body that includes the original message text.

The example ends with the reply message being sent via SmtpClient.

Screenshot
Reply Screenshot

See the full code below.

using GemBox.Email;
using GemBox.Email.Imap;
using GemBox.Email.Mime;
using GemBox.Email.Pop;
using GemBox.Email.Smtp;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        MailMessage message;

        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
        {
            imap.Connect();
            Console.WriteLine("Connected.");

            imap.Authenticate("<USERNAME>", "<PASSWORD>");
            Console.WriteLine("Authenticated.");

            imap.SelectInbox();
            IList<int> results = imap.SearchMessageNumbers("FROM sender@example.com SUBJECT " +
                                                           "\"Are you coming to the party\"");

            if (results.Count == 0)
                Console.WriteLine("Message not found.");

            message = imap.GetMessage(results[0]);
        }

        MailAddress fromAddress = message.From[0];
        MailMessage reply = new MailMessage(new MailAddress("receiver@example.com", "Receiver"),
                                            fromAddress);

        // Add 'In-Reply-To' header
        Header header = new Header(HeaderId.InReplyTo, message.Id);
        reply.MimeEntity.Headers.Add(header);

        // Check if 'References' header exists and its body is not null or empty
        if (message.MimeEntity.Headers.TryGetHeader(HeaderId.References, out header) && !string.IsNullOrEmpty(header.Body))
            // If found, copy original value and append last message id
            header = new Header(HeaderId.References, header.Body + ' ' + message.Id);
        else
            // If not found, create one with message id
            header = new Header(HeaderId.References, message.Id);

        // Add 'References' header to reply message
        reply.MimeEntity.Headers.Add(header);

        // Set subject
        reply.Subject = "Re: " + message.Subject;

        // Compose text body
        StringBuilder text = new StringBuilder();

        text.AppendLine("Yes. See you there at 9.");
        text.AppendLine();
        text.AppendFormat("On {0}, {1} wrote:", message.Date.ToString(CultureInfo.InvariantCulture), fromAddress);
        text.AppendLine();

        // Append original text body if it exists
        if (message.BodyText != null)
        {
            text.AppendLine();
            text.Append("> " + message.BodyText.Replace("\r\n", "\r\n> "));
        }

        reply.BodyText = text.ToString();

        // Compose HTML body
        text = new StringBuilder();

        text.Append("<div>Yes. See you there at 9.</div><br/>");
        text.AppendFormat("<div>On {0}, ", message.Date.ToString(CultureInfo.InvariantCulture));

        if (fromAddress.DisplayName != null)
            text.Append(fromAddress.DisplayName + ' ');

        text.AppendFormat("<<a href=\"mailto:{0}\">{0}</a>> wrote:<br/>", fromAddress.Address);

        // Append original text body if it exists
        if (message.BodyHtml != null)
        {
            text.Append("<blockquote style=\"margin: 0 0 0 5px;");
            text.Append(" border-left: 2px blue solid;");
            text.Append(" padding-left: 5px\">");
            text.Append(message.BodyHtml);
            text.Append("</blockquote><br/></div>");
        }

        reply.BodyHtml = text.ToString();

        Console.WriteLine("Sending reply...");

        // Initialize new SMTP client and send message
        using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)"))
        {
            smtp.Connect();
            Console.WriteLine("Connected.");

            smtp.Authenticate("<USERNAME>", "<PASSWORD>");
            Console.WriteLine("Authenticated.");

            smtp.SendMessage(message);
        }

        Console.WriteLine("Reply sent successfully.");
    }
}
Imports System.Globalization
Imports System.Text
Imports GemBox.Email
Imports GemBox.Email.Imap
Imports GemBox.Email.Mime
Imports GemBox.Email.Pop
Imports GemBox.Email.Smtp

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim message As MailMessage

        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")
            imap.Connect()
            Console.WriteLine("Connected.")

            imap.Authenticate("<USERNAME>", "<PASSWORD>")
            Console.WriteLine("Authenticated.")

            imap.SelectInbox()
            Dim results As IList(Of Integer) = imap.SearchMessageNumbers("FROM sender@example.com SUBJECT " &
                                                                         """Are you coming to the party""")

            If results.Count = 0 Then
                Console.WriteLine("Message Not found.")
            End If

            message = imap.GetMessage(results(0))
        End Using

        Dim fromAddress As MailAddress = message.From(0)
        Dim reply As New MailMessage(New MailAddress("receiver@example.com", "Receiver"),
                                     fromAddress)

        ' Add 'In-Reply-To' header
        Dim header As New Header(HeaderId.InReplyTo, message.Id)
        reply.MimeEntity.Headers.Add(header)

        ' Check if 'References' header exists and its body is not null or empty
        If message.MimeEntity.Headers.TryGetHeader(HeaderId.References, header) AndAlso Not String.IsNullOrEmpty(header.Body) Then
            ' If found, copy original value and append last message id
            header = New Header(HeaderId.References, header.Body & " "c & message.Id)
        Else
            ' If not found, create one with message id
            header = New Header(HeaderId.References, message.Id)
        End If

        ' Add 'References' header to reply message
        reply.MimeEntity.Headers.Add(header)

        ' Set subject
        reply.Subject = "Re:  " + message.Subject

        ' Compose text body
        Dim text As New StringBuilder()

        text.AppendLine("Yes. See you there at 9.")
        text.AppendLine()
        text.AppendFormat("On {0}, {1} wrote:", message.Date.ToString(CultureInfo.InvariantCulture), fromAddress)
        text.AppendLine()

        ' Append original text body if it exists
        If message.BodyText IsNot Nothing Then
            text.AppendLine()
            text.Append("> " + message.BodyText.Replace(vbCrLf, vbCrLf & "> "))
        End If

        reply.BodyText = text.ToString()

        ' Compose HTML body
        text = New StringBuilder()

        text.Append("<div>Yes. See you there at 9.</div><br/>")
        text.AppendFormat("<div>On {0}, ", message.Date.ToString(CultureInfo.InvariantCulture))

        If fromAddress.DisplayName IsNot Nothing Then
            text.Append(fromAddress.DisplayName + " "c)
        End If

        text.AppendFormat("<<a href=""mailto:{0}"">{0}</a>> wrote:<br/>", fromAddress.Address)

        ' Append original text body if it exists
        If message.BodyHtml IsNot Nothing Then
            text.Append("<blockquote style=""margin: 0 0 0 5px;")
            text.Append(" border-left 2px blue solid;")
            text.Append(" padding-left 5px"">")
            text.Append(message.BodyHtml)
            text.Append("</blockquote><br/></div>")
        End If

        reply.BodyHtml = text.ToString()

        Console.WriteLine("Sending reply...")

        ' Initialize new SMTP client and send message
        Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")
            smtp.Connect()
            Console.WriteLine("Connected.")

            smtp.Authenticate("<USERNAME>", "<PASSWORD>")
            Console.WriteLine("Authenticated.")

            smtp.SendMessage(message)
        End Using

        Console.WriteLine("Reply sent successfully.")

    End Sub

End Module

Check next sample.