存储VB.Net应用程序的SMTP凭据

时间:2010-04-08 14:55:46

标签: vb.net smtp web-config security credentials

sHi folk,

我一直在web.conf中存储SQL连接字符串,这很好,但现在我需要将SMTP凭据存储在受保护的地方。 web.conf似乎是受保护的最有可能的地方,但它们如何存储?

我已将详细信息添加到我的web.conf但不确定如何引用它们

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

发送邮件:

      Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("billy.jones@networkroi.co.uk")
        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)

- 琼西

1 个答案:

答案 0 :(得分:4)

您可以采取一些方法。每个都有其优点。

  • 如果您希望服务器凭据可配置,则应将它们存储在数据库表中。
  • 如果您认为它们是相当静态的,但您不想重新编译代码来更改它们,请使用web.config(或适用时的app.config)。
  • 如果您希望它们可以在服务器之间进行配置,您还可以查看注册表。

如果您具体询问如何在web.config文件中存储SMTP凭据,您可以执行以下操作:

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

如果您需要帮助从appSettings中获取值check out this article