The "Mailaddress.Displayname" property or indexer cannot be assigned, as it is read-only

Asked

Viewed 94 times

0

I can already send an email normally. I just can’t change DisplayName.

Follows code :

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

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

1 answer

2


You need to create an object to use on 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 put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.