Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

email

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]");

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);

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]"));

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 );