【问题标题】:Moving ASP.Net System.Net.Mail settings to Web.config将 ASP.Net System.Net.Mail 设置移动到 Web.config
【发布时间】:2012-11-16 13:52:45
【问题描述】:

我们正在使用此编码从 ASP.Net 代码隐藏 Vb.Net 文件发送电子邮件。

这些编码中的任何一个都可以放在 Web.config 文件中吗?

Protected Sub EmailStudentList()

    ' Get the rendered HTML.
    '-----------------------
    Dim SB As New StringBuilder()
    Dim SW As New StringWriter(SB)
    Dim htmlTW As New HtmlTextWriter(SW)

    GridViewSummary.RenderControl(htmlTW)

    ' Get the HTML into a string.
    ' This will be used in the body of the email report.
    '---------------------------------------------------
    Dim dataGridHTML As String = SB.ToString()

    Dim SmtpServer As New SmtpClient()
    SmtpServer.Credentials = New Net.NetworkCredential("ourEmailUsername@gmail.com", "ourPassword")
    SmtpServer.Port = 587
    SmtpServer.Host = "smtp.gmail.com"
    SmtpServer.EnableSsl = True

    ObjMailMessage = New MailMessage()

    Try
        ObjMailMessage.From = New MailAddress("ourEmail@gmail.com", "Some text is here.", System.Text.Encoding.UTF8)
        ObjMailMessage.To.Add(New MailAddress("BoardOfDirectors@gmail.com", "Emad-ud-deen", System.Text.Encoding.UTF8))
        ObjMailMessage.Subject = "List of enrolled students for the board of directors"
        ObjMailMessage.Body = dataGridHTML
        ObjMailMessage.IsBodyHtml = True
        ObjMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

        SmtpServer.Send(ObjMailMessage)

    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try
End Sub

【问题讨论】:

    标签: asp.net vb.net email web-config settings


    【解决方案1】:
      <system.net>
        <mailSettings>
          <smtp from="ourEmail@gmail.com">
            <network defaultCredentials="false" 
                 userName="ourEmailUsername@gmail.com" 
                 password="ourPassword" 
                 host="smtp.gmail.com" 
                 enableSsl="true" 
                 port="587"/>
          </smtp>
        </mailSettings>
      </system.net>
    

    【讨论】:

    • 非常感谢您的快速回复!非常有帮助。 :-)
    【解决方案2】:

    您不能在配置文件中放置“代码”,但可以移动一些设置。

    http://msdn.microsoft.com/en-us/library/w355a94k.aspx

    <configuration>
      <system.net>
        <mailSettings>
          <smtp deliveryMethod="network">
            <network
              host="localhost"
              port="25"
              defaultCredentials="true"
            />
          </smtp>
        </mailSettings>
      </system.net>
    </configuration>
    

    【讨论】:

    • 很高兴为您提供帮助。看起来我们都得到了同样令人放心的答案。
    【解决方案3】:

    我已将其中一些项目放在 web.config` 中。

    <system.net>
        <mailSettings>
            <smtp>
                <network host="<<Host IP Address>>" port="<<Host Port Number>>" userName="" password=""/>
            </smtp>
        </mailSettings>
        <defaultProxy useDefaultCredentials="false">
            <proxy bypassonlocal="true" usesystemdefault="false"/>
        </defaultProxy>
    </system.net>
    

    以下链接也可能有所帮助:

    Element (Network Settings)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 2010-12-06
      • 2011-12-16
      • 2011-03-09
      • 2011-05-15
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多