60

I want to set the ReplyTo value for a .NET MailMessage.

MailMessage.ReplyTo Property:

ReplyTo is obsoleted for this type. Please use ReplyToList instead which can accept multiple addresses.

MailMessage.ReplyToList Property:

Gets or sets the list of addresses to reply to for the mail message.

But, ReplyToList is ReadOnly.

I've tried to use the MailMessage.Headers property like this:

mail.Headers.Add("Reply-To", "johndoe@example.com");

as described here: System.Web.Mail, OH MY!

But, that doesn't seem to work.

How do I set the value(s) of the MailMessage's ReadOnly property ReplyToList?

Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
  • This is exactly why we need [**Warlords of Documentation**](http://meta.stackoverflow.com/questions/306213/warlords-of-documentation-your-questions-answered?cb=1) - This is infinitely more helpful than the MSDN page on [`MailMessage.ReplyToList`](https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.replytolist(v=vs.110).aspx) – KyleMit Sep 23 '15 at 15:58

7 Answers7

127

ReplyToList is an instance of MailAddressCollection which exposes Add method.

To add a new address you can simply pass address as string

  message.ReplyToList.Add("john.doe@example.com");
Giorgi
  • 30,270
  • 13
  • 89
  • 125
  • 3
    Bah! That's just obtuse. Why not allow an assignment of a collection of `MailAddress`?? – SteveCinq Jul 05 '18 at 02:19
  • Note: You can pass a single string containing a comma-separated list of email addresses if necessary ;) – Zeek2 Jun 12 '19 at 10:55
11

I like the array init syntax, which will call Add() for you.

var msg = new MailMessage("noreply@example.com", mailTo) {
    Subject = "my important message",
    Body = this.MessageBody,
    ReplyToList = { mailTo } // array init syntax calls Add()
};
mailClient.Send(msg);
yzorg
  • 4,224
  • 3
  • 39
  • 57
7

You cannot say

message.ReplyToList = new MailAddressCollection();

To create a new collection. However, adding to the existing collection is what you want to do.

message.ReplyToList.Add(new MailAddress("foo@bar.net"));
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
6

My answer is not unlike the accepted answers already given. However, I felt it needed to be provided.

var fromEmail = new MailAddress("foobar@example.com", "Foo Bar");
var replyEmail = new MailAddress("foo@example.com", "Foo Example");
var msgEmail = new MailMessage { From = fromEmail };
msgEmail.ReplyToList.Add( replyEmail );
Taersious
  • 751
  • 9
  • 20
3

I used the MailMessage.Sender property instead.

mail.Sender = new Mail.MailAddress("system@example.com");
mail.From = new Mail.MailAddress("johndoe@example.com", "John Doe");

More info: MailMessage, difference between Sender and From properties

Community
  • 1
  • 1
Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
1
MailMessage.ReplyToList.Add("johndoe@example.com, janedoe@example.com, xxxx@yyyy.com");

Ref. https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailaddresscollection.add?redirectedfrom=MSDN&view=netframework-4.8#overloads


I expect that it is also possible to optionally include "friendly name" using the above approach, probably using this format: Jon Doe <jondoe@example.com>

Refs.: https://askleo.com/why_are_email_addresses_sometimes_in_anglebrackets/, https://help.returnpath.com/hc/en-us/articles/220563147-What-is-a-Friendly-From-name-and-address-, https://wordtothewise.com/2014/12/friendly-email-addresses/.

Zeek2
  • 386
  • 4
  • 8
0

You need to add list of ReplyTo Addresses to ReplyToList by Add method :

            mail.Sender = new MailAddress(from, displayName);
            mail.From = new MailAddress(from, displayName);
            mail.ReplyToList.Add("replyToAddress@host.com");