Search code examples
c#asp.net-mvcemailmvcmailer

Proper way to change DisplayName of Email in MVCMailer


I looking to change the display name in a Email using MVCMailer. Instead of the client seeing From: [email protected] they will see "xyzCompany Customer Service". I have looked all around the internet and can not find any documentation that explains how.

USERMAILER.CS

public virtual MvcMailMessage Welcome(string sentTo, string replyTo)
        {

            return Populate(x =>
            {
                x.Subject = "Welcome";
                x.ViewName = "Welcome"; //View name of email going out.
                x.ReplyToList.Clear();
                x.ReplyToList.Add(replyTo);
                x.To.Add(sentTo);
                x.From.DisplayName("xyz Company Customer Service"); 
                x.From = new MailAddress("[email protected]");
                x.ViewName = "WelcomeEmail"; //View name of email going out.
            });
         }

The line 'x.From.DisplayName("xyz Company Customer Service")' gives me an error: system.net.mail.mailaddress.DisplayName can not be used as a method.

Can anyone please tell me how to properly change the displayname?


Solution

  • DisplayName is a property of the MailAddress class. You can use this overload of the constructor to specify it:

    x.From = new MailAddress(address: "[email protected]", displayName: "xyz Company Customer Service");
    

    Update based on comment:

    The DisplayName property has no (or a private) setter, meaning you can only set it through the constructor of MailAddress, but not through the property itself.