.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
How do I assign a value to a MailMessage ReplyTo property?
提问by Zack Peterson
I want to set the ReplyTo value for a .NET MailMessage.
我想为 .NET MailMessage 设置 ReplyTo 值。
MailMessage.ReplyToProperty:
ReplyTo is obsoleted for this type. Please use ReplyToList instead which can accept multiple addresses.
ReplyTo 对于此类型已过时。请改用 ReplyToList,它可以接受多个地址。
MailMessage.ReplyToListProperty:
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
回答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]");
回答by Zeek2
MailMessage.ReplyToList.Add("[email protected], [email protected], [email protected]");
I expect that it is also possible to optionally include "friendly name" using the above approach, probably using this format:Jon Doe <[email protected]>
我希望也可以使用上述方法选择性地包含“友好名称”,可能使用这种格式:Jon Doe <[email protected]>
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/.
参考文献: 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/。

