vb.net 将 ASP.Net System.Net.Mail 设置移动到 Web.config

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13609717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:21:23  来源:igfitidea点击:

Moving ASP.Net System.Net.Mail settings to Web.config

asp.netvb.netemailweb-configsettings

提问by Emad-ud-deen

We are using this coding to send email from an ASP.Net code-behind Vb.Net file.

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

Can any of this coding be placed inside the Web.config file?

可以将这些编码中的任何一个放置在 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("[email protected]", "ourPassword")
    SmtpServer.Port = 587
    SmtpServer.Host = "smtp.gmail.com"
    SmtpServer.EnableSsl = True

    ObjMailMessage = New MailMessage()

    Try
        ObjMailMessage.From = New MailAddress("[email protected]", "Some text is here.", System.Text.Encoding.UTF8)
        ObjMailMessage.To.Add(New MailAddress("[email protected]", "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

回答by matt-dot-net

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network defaultCredentials="false" 
             userName="[email protected]" 
             password="ourPassword" 
             host="smtp.gmail.com" 
             enableSsl="true" 
             port="587"/>
      </smtp>
    </mailSettings>
  </system.net>

回答by andleer

You can't place "code" in your config file but you can move a number of the settings.

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

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

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>

回答by DaveB

I have put some of these items in web.config`.

我已经将其中一些项目放在 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>

The following link may help as well:

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

Element (Network Settings)

元素(网络设置)