.net 如何为 MailMessage ReplyTo 属性赋值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2758699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:14:13  来源:igfitidea点击:

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

.netemail

提问by Zack Peterson

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

我想为 .NET MailMessage 设置 ReplyTo 值。

MailMessage.ReplyToProperty:

MailMessage.ReplyTo属性:

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

ReplyTo 对于此类型已过时。请改用 ReplyToList,它可以接受多个地址。

MailMessage.ReplyToListProperty:

MailMessage.ReplyToList属性:

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

获取或设置邮件消息要回复的地址列表。

But, ReplyToList is ReadOnly.

但是, ReplyToList 是只读的。

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

我试过像这样使用MailMessage.Headers属性:

mail.Headers.Add("Reply-To", "[email protected]");

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

如此处所述:System.Web.Mail,天哪!

But, that doesn't seem to work.

但是,这似乎行不通。

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

如何设置 MailMessage 的 ReadOnly 属性 ReplyToList 的值?

回答by Giorgi

ReplyToListis an instance of MailAddressCollectionwhich exposes Addmethod.

ReplyToList是公开Add方法的MailAddressCollection 的一个实例。

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

要添加新地址,您只需将地址作为字符串传递

  message.ReplyToList.Add("[email protected]");

回答by yzorg

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

我喜欢数组初始化语法,它会为你调用 Add()。

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

回答by Taersious

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

回答by Anthony Pegram

You cannot say

你不能说

message.ReplyToList = new MailAddressCollection();

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

创建一个新的集合。但是,添加到现有集合是您想要做的。

message.ReplyToList.Add(new MailAddress("[email protected]"));

回答by Zack Peterson

I used the MailMessage.Senderproperty instead.

我改用了MailMessage.Sender属性。

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

更多信息:MailMessage,Sender 和 From 属性之间的区别

回答by Adel P.G.

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

您需要通过 Add 方法将 ReplyTo 地址列表添加到 ReplyToList :

            mail.Sender = new MailAddress(from, displayName);
            mail.From = new MailAddress(from, displayName);
            mail.ReplyToList.Add("[email protected]");