Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

aspnet credentials from web.config

$
0
0
To send emails from an (old) asp.net website I use the following code :    
-------

    Protected Sub SendMail(sender As Object, e As System.EventArgs)
        Try
            Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
            Dim smtp As New SmtpClient
            smtp.Host = smtpSection.Network.Host
            smtp.EnableSsl = smtpSection.Network.EnableSsl
            smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
            smtp.Port = smtpSection.Network.Port'solution 1 does not work !'Dim networkCred As NetworkCredential = New Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)'smtp.Credentials = networkCred'solution 2 does not work !'Dim networkCred As New Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)'smtp.Credentials = networkCred'solution 3 does work !!!
            smtp.Credentials = New Net.NetworkCredential("yyyyyy@xxxxxx.com", "zzzzzz")
            Using mm As New MailMessage(smtpSection.From, txtTo.Text.Trim())
                mm.Subject = txtSubject.Text.Trim()
                mm.Body = txtBody.Text.Trim()
                mm.IsBodyHtml = False
                TextBox2.Text = smtpSection.Network.UserName
                TextBox3.Text = smtpSection.Network.Password
                TextBox4.Text = smtpSection.Network.Host
                TextBox5.Text = smtpSection.Network.Port
                TextBox6.Text = smtpSection.Network.EnableSsl
                TextBox7.Text = smtpSection.Network.DefaultCredentials
                TextBox8.Text = smtpSection.From
                TextBox9.Text = txtTo.Text.Trim()
                smtp.Send(mm)

            End Using
            
        Catch error_t As Exception
            TextBox1.Text = error_t.ToString
        End Try
        
    End Sub
-------

The web.config section contains this : 
-------

      <smtp deliveryMethod="Network" from="yyyyyy@xxxxxx.com" ><network
            host="smtp-auth.mailprotect.be"
            port="2525"
            userName="yyyyyyyy@xxxxxxxx.com "
            password="zzzzzz"
            defaultCredentials="false"
            enableSsl="false"
        /></smtp>
------

The namespaces loaded are : 

    <%@ Import Namespace="System.Net" %><%@ Import Namespace="System.Net.Mail" %><%@ Import Namespace="System.Configuration" %><%@ Import Namespace="System.Net.Configuration" %>

Solution 3 works fine but I don't like to show passwords in a page.

When using solution 1 or 2 the mailserver replies it does not relay, so apparently the credentials are not retrieved work properly.
All the other parameters do show properly in the textboxes I designed to test this sub.

Does anyone have a suggestion ?

Thanks.

Philippe Caron

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>