Emad-ud-deen
Emad-ud-deen

Reputation: 4864

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

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

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

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

Upvotes: 4

Views: 5847

Answers (3)

DaveB
DaveB

Reputation: 9530

I have put some of these items in 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)

Upvotes: 2

andleer
andleer

Reputation: 22578

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

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

Upvotes: 2

matt-dot-net
matt-dot-net

Reputation: 4244

  <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>

Upvotes: 12

Related Questions