How do I assign a value to a MailMessage ReplyTo property?

.NetEmail

.Net Problem Overview


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", "[email protected]");

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?

.Net Solutions


Solution 1 - .Net

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("[email protected]");

Solution 2 - .Net

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

var msg = new MailMessage("[email protected]", mailTo) {
    Subject = "my important message",
    Body = this.MessageBody,
    ReplyToList = { mailTo } // array init syntax calls Add()
};
mailClient.Send(msg);

Solution 3 - .Net

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("[email protected]"));

Solution 4 - .Net

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

var fromEmail = new MailAddress("[email protected]", "Foo Bar");
var replyEmail = new MailAddress("[email protected]", "Foo Example");
var msgEmail = new MailMessage { From = fromEmail };
msgEmail.ReplyToList.Add( replyEmail );

Solution 5 - .Net

I used the MailMessage.Sender property instead.

mail.Sender = new Mail.MailAddress("[email protected]");
mail.From = new Mail.MailAddress("[email protected]", "John Doe");

More info: MailMessage, difference between Sender and From properties

Solution 6 - .Net

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("[email protected]");

Solution 7 - .Net

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZack PetersonView Question on Stackoverflow
Solution 1 - .NetGiorgiView Answer on Stackoverflow
Solution 2 - .NetyzorgView Answer on Stackoverflow
Solution 3 - .NetAnthony PegramView Answer on Stackoverflow
Solution 4 - .NetTaersiousView Answer on Stackoverflow
Solution 5 - .NetZack PetersonView Answer on Stackoverflow
Solution 6 - .NetAdel P.G.View Answer on Stackoverflow
Solution 7 - .NetZeek2View Answer on Stackoverflow