The property or indexer "MailAddress.DisplayName" can not be assigned because it is read-only

0

I can usually send an email. I just can not change DisplayName .

Follow the code below:

var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
var message = new MailMessage();
message.From.DisplayName = "Nome Nome";
message.To.Add(new MailAddress("[email protected]"));
message.Subject = "Teste Subject";
message.Body = string.Format(body);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
    await smtp.SendMailAsync(message);
}

At line message.From.DisplayName = "Nome Nome"; , how can I change DisplayName ?

    
asked by anonymous 16.08.2017 / 23:27

1 answer

2

You need to create an object to use in From :

var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
var message = new MailMessage();
message.From = new MailAddress("[email protected]", "Nome Nome");
message.To.Add(new MailAddress("[email protected]"));
message.Subject = "Teste Subject";
message.Body = string.Format(body);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient()) {
    await smtp.SendMailAsync(message);
}

I placed GitHub for future reference .

    
16.08.2017 / 23:36