Reply to Email in C# and VB.NET

To write a reply message, your email needs to contain two headers: In-Reply-To and References. These headers, also known as "identification fields", are defined in RFC 2822.

Optionally, you can prefix the email's subject with "Re:" and include the original message in the reply email's body.

The following example shows how to search for a specific message in the INBOX folder using ImapClient. A reply email is created in C# and VB.NET with the required reply headers and a HTML body that includes the original message text, after which the message is sent using SmtpClient.

Screenshot of replied email containing original message
Reply to an email in C# and VB.NET
using System.Linq;
using GemBox.Email;
using GemBox.Email.Imap;
using GemBox.Email.Mime;
using GemBox.Email.Smtp;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Retrieve searched email.
        MailMessage originalMessage;
        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
        {
            imap.Connect();
            imap.Authenticate("<USERNAME>", "<PASSWORD>");
            imap.SelectInbox();

            string search = @"SUBJECT ""Are you coming to the party?""";
            originalMessage = imap.GetMessage(imap.SearchMessageNumbers(search).First());
        }

        // Create reply email with sender and receiver addresses swapped.
        MailMessage replyMessage = new MailMessage(
            originalMessage.To[0],
            originalMessage.From[0]);

        // Add 'In-Reply-To' and 'References' header.
        replyMessage.MimeEntity.Headers.Add(
            new Header(HeaderId.InReplyTo, originalMessage.Id));
        replyMessage.MimeEntity.Headers.Add(
            new Header(HeaderId.References, originalMessage.Id));

        // Set subject.
        replyMessage.Subject = $"Re: { originalMessage.Subject }";

        // Set reply message message.
        replyMessage.BodyHtml = "<div>Yes. See you there at 9.</div>";

        // Append original message text.
        replyMessage.BodyHtml +=
            $"<div>On {originalMessage.Date:G}, {originalMessage.From[0].Address} wrote:</div>" +
            $"<blockquote>{originalMessage.BodyHtml}</blockquote>";

        // Send reply email.
        using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)"))
        {
            smtp.Connect();
            smtp.Authenticate("<USERNAME>", "<PASSWORD>");
            smtp.SendMessage(replyMessage);
        }
    }
}
Imports System.Linq
Imports GemBox.Email
Imports GemBox.Email.Imap
Imports GemBox.Email.Mime
Imports GemBox.Email.Smtp

Module Program

    Sub Main()

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

        ' Retrieve searched email.
        Dim originalMessage As MailMessage
        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")

            imap.Connect()
            imap.Authenticate("<USERNAME>", "<PASSWORD>")
            imap.SelectInbox()

            Dim search As String = "SUBJECT ""Are you coming to the party?"""
            originalMessage = imap.GetMessage(imap.SearchMessageNumbers(search).First())
        End Using

        ' Create reply email with sender and receiver addresses swapped.
        Dim replyMessage As New MailMessage(
            originalMessage.To(0),
            originalMessage.From(0))

        ' Add 'In-Reply-To' and 'References' header.
        replyMessage.MimeEntity.Headers.Add(
            New Header(HeaderId.InReplyTo, originalMessage.Id))
        replyMessage.MimeEntity.Headers.Add(
            New Header(HeaderId.References, originalMessage.Id))

        ' Set subject.
        replyMessage.Subject = $"Re: { originalMessage.Subject }"

        ' Set reply message message.
        replyMessage.BodyHtml = "<div>Yes. See you there at 9.</div>"

        ' Append original message text.
        replyMessage.BodyHtml +=
            $"<div>On {originalMessage.Date:G}, {originalMessage.From(0).Address} wrote:</div>" +
            $"<blockquote>{originalMessage.BodyHtml}</blockquote>"

        ' Send reply email.
        Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")
            smtp.Connect()
            smtp.Authenticate("<USERNAME>", "<PASSWORD>")
            smtp.SendMessage(replyMessage)
        End Using

    End Sub
End Module

Even though there is no standard that defines the format for quoting the original message, for a HTML message, the common approach is to use the <blockquote> tag (as shown in above example) with some styling, and for a plain text message the common approach is to quote every line of text by prefixing it with the '>' character.

If the email has a reply chain or multiple reply messages, then a References header should contain those message IDs separated with the space character.

Also, you can use the MailMessage.ReplyTo collection to specify the list of addresses for the email reply. In other words, when clicking to a 'reply' in your email client, the MailMessage.ReplyTo addresses should be used as the recipient addresses instead of the usual MailMessage.From addresses.

Check next example or download examples from GitHub.