Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a mail as a reply using SmtpClient

Scenario : Need to send a mail which is actually a reply mail from an asp.net c# program. I managed the mail to be sent to the client, but it sends as a new mail.

Code :

var SMTP = _genRepository.GetData("SELECT * FROM LOCATION WHERE ID='" + mail.LocationId + "'").FirstOrDefault();
SmtpClient c = new SmtpClient(SMTP.SMTP_Host, SMTP.SMTP_Port);
MailAddress add = new MailAddress(mail.From);
MailMessage msg = new MailMessage();
msg.To.Add(add);
msg.From = new MailAddress(SMTP.Email);
msg.IsBodyHtml = true;
msg.Subject = mail.Subject;
msg.Body = mail.Body;
c.Credentials = new System.Net.NetworkCredential(SMTP.Email, SMTP.EmailPassword);
c.EnableSsl = true;
c.Send(msg);

I have the sender's email messageid. I just need to know how to send the mail as a reply.

like image 395
Ranjith Varatharajan Avatar asked Jan 01 '26 02:01

Ranjith Varatharajan


1 Answers

If you add following headers, the mail client would consider the mail as a reply.

In-Reply-To

References

        MailMessage mailMessage = new MailMessage();
        mailMessage.Headers.Add("In-Reply-To", "<Message-ID Value>");
        mailMessage.Headers.Add("References", "<Message-ID Value>");

I could not find any 'official' reference for the SMTP headers, but the following gives some details:

The presence of In-Reply-To and References headers indicate that the message is a reply to a previous message.

The References header makes "threaded mail reading" and per-discussion archival possible.

Also, some mail clients wants the exact same subject as well. Please see this related SO post

In outlook, if the the mail subject is same and "conversation view" is enabled for the folder, then irrespective of the above headers, it would group all mails with the same subject together.

You can send a reply using your client manually and compare the message headers with the original mail to see how your client is adding the message headers.

like image 176
Subbu Avatar answered Jan 03 '26 16:01

Subbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!