C# MailMessage 设置发件人姓名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2231917/
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-08-07 00:36:27  来源:igfitidea点击:

MailMessage setting the Senders Name

c#

提问by James Jeffery

Is it possible to set the sender name on a MailMessageobject? I tried setting it from MailAddress, but the DisplayNameproperty seems to be read only.

是否可以在MailMessage对象上设置发件人姓名?我尝试从 设置它MailAddress,但该DisplayName属性似乎是只读的。

I tried "My Name " as the sender and don't seem to work either.

我尝试将“我的名字”作为发件人,但似乎也不起作用。

采纳答案by Ta01

MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]", "Bob Jones" );

回答by decompiled

From the MSDN http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx

从 MSDN http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx

MailMessage message = new MailMessage(
           "[email protected]",
           "[email protected]",
           "Quarterly data report.",
           "See the attached spreadsheet.");

回答by loraderon

You do not need to use the MailAddress class.

您不需要使用 MailAddress 类。

You can let the runtime parse your string.

您可以让运行时解析您的字符串。

var message = new MailMessage(
    "My Name [email protected]", 
    "Recipient One [email protected],Recipient Two [email protected]",
    "Subject",
    "Body");