views:

141

answers:

3

I'm working on an EmailSender, and I'm grabbing the email address from my Web.config file. I'd like to also grab a "display name" for that email, from the same section if possible, but I'm not seeing an obvious way to do this.

In my Web.config file, I have included a default "from email address", like this:

<configuration>
  <system.net>
    <mailSettings>
      <smtp from="[email protected]"><!-- no displayName attribute :( -->
        <network ... />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

In my EmailSender, I have something like this:

        var smtpSection = ...;
        var message = new MailMessage();
        message.From =  new MailAddress(_settings.From, senderDisplayName);

Is there a recommended way to store senderDisplayName in a web.config file?

  • Is there some way to include it in the from attribute? For example:

    <smtp from="Automatic Mailer &lt;[email protected]&gt;">
    
  • Or does it need to be a custom element in appSettings?
  • Or is there some other way?
A: 

Do you HAVE to pull the email address etc from the web.config? Why not set it in the EmailSender?

The MailAddress constructor also includes an overload that allows the display name to be specified (New MailAddress(address, displayName). Most email clients display the display name, if present, instead of the email address.

http://www.4guysfromrolla.com/articles/101707-1.aspx

Albert
I would prefer it in Web.config because the information might change (e.g., people might think they're receiving spam with the current name--might need to try other ideas). Bottom line: I might need to change the info after deployment.
DanM
A: 

Personally, I set it in the Web.Config in the AppSettings section, since the smtp settings section doesn't provide a place for it.

David Stratton
+3  A: 

Here check this thread, might be of help:

http://stackoverflow.com/questions/1394354/storing-smtp-from-email-friendly-display-name-in-web-config

Albert
Good answer. +1 for finding it.
David Stratton