3

I looking to change the display name in a Email using MVCMailer. Instead of the client seeing From: custmerservice@xyzCompany.com 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("customerservice@xyzCompany.com");
                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?

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
user2789697
  • 227
  • 1
  • 7
  • 13

1 Answers1

3

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

x.From = new MailAddress(address: "customerservice@xyzCompany.com", 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.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • Thank you so much. It worked! However, the second option: x.From.DisplayName = "xyz Company Customer Service"; does not work for me. It is now giving me an error saying it's readonly. – user2789697 Oct 09 '13 at 19:57
  • @user2789697 you're welcome. This means that `DisplayName` is read-only, I updated my answer. – Henk Mollema Oct 09 '13 at 20:12
  • @user2789697 also, please mark this as the answer for people with the same question. – Henk Mollema Oct 09 '13 at 20:18