views:

361

answers:

1

sHi folk,

I've been storing SQL connection strings in web.conf which is fine but now I need to store SMTP credentials somewhere protected. web.conf seems like the likeliest place since its protected but how can they be stored?

I've added the details to my web.conf but amnot sure how to reference them

<system.net>
   <mailSettings>
   <smtp>
    <network 
      host ="server"
      userName ="username"
      password ="password"
      defaultCredentials =" false"
      port =" 25"
    />
  </smtp>
  </mailSettings>
</system.net>

Sending the mail:

      Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("[email protected]")
        mail.To.Add(ToAddress)

        'set the content
        mail.Subject = "User Request Submitted via Client Portal"
        mail.Body = "text in here"
        mail.IsBodyHtml = True

        ' authenticatin
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential("username", "-password-")


        'send the message
        Dim smtp As New SmtpClient("servername")
        smtp.UseDefaultCredentials = False
        smtp.Credentials = basicAuthenticationInfo

        smtp.Send(mail)

-- Jonesy

+2  A: 

You can take a few approaches. Each has its merits.

  • If you want the server credentials to be configurable, you should store them in a database table.
  • If you think they will be fairly static, but you don't want to have to recompile code to change them, use web.config (or app.config when applicable).
  • You could also look into registry if you want them to be configurable from server to server.

In case you were asking specifically how to store SMTP credentials in a web.config file, you could do something like this :

<configuration>
   <appSettings>
      <add key="SMTP_Server" value="my.smtpserver.com" />
      <add key="SMTP_Username" value="myusername" />
      <add key="SMTP_Password" value="mypassword" />
   </appSettings>
</configuration>

If you need help getting values out of appSettings, check out this article.

Byron Sommardahl
thanks for the reply Byron! I want to go with your second bullet point but am not sure how to do it. I have found an example that will store the details in web.conf but how do i refer to these in the code behind? I've updated my post with the web.conf code.
iamjonesy
Byron's example gives you the answer: storing them in appSettings keys is easiest. To retrieve a key's value, use `ConfigurationManager.AppSettings("[key]")`
tloflin
forget my last comment, the answer is you DON'T need to refer to the smtp settings in web.conf they are used automtatically. @tloflin - I'm going to try out appSettings too thanks!
iamjonesy
@Jonesy: Interesting, I didn't know that an SmtpClient would automatically retrieve settings from the config file. In that specific case, then, that's better. For general purposes, appSettings is nice.
tloflin
@tloflin yeah I know very handy indeed, Dim smtp As New SmtpClient() _ smtp.Send(mail) is enough to go get smtp settings :)
iamjonesy