iamjonesy
iamjonesy

Reputation: 25132

Storing SMTP credentials for a VB.Net application

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

Upvotes: 1

Views: 7046

Answers (1)

Byron Sommardahl
Byron Sommardahl

Reputation: 13012

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.

Upvotes: 4

Related Questions