0

being a novice (and certainly not good in English) I need some help. I am a VB-coder. Not sure I have fully understood the instructions given how to fill in the question.

I have X mail accounts on the server and when sending mail I want to use the sender's own account

Therefor in code behind I have following code which works.

SELECT CASE Sender
case is = Name1
    SmtpServer.Credentials = New System.Net.NetworkCredential("Name1@mywebsite.se", "Pwd1")
case is = Name2
    SmtpServer.Credentials = New System.Net.NetworkCredential("Name2@mywebsite.se", "Pwd2")
case is = NameX
    SmtpServer.Credentials = New System.Net.NetworkCredential("NameX1@mywebsite.se", "PwdX")
END SELECT

And for sending the mail:

Protected Sub SkickaMail(receiver As String, SavePath As String)
     Dim mailWebb As New Net.Mail.MailMessage

    Using mailWebb
        mailWebb.To.Add(receiver)
        mailWebb.From = New Net.Mail.MailAddress(sender)
        If ChkKvitto.Checked Then
            mailWebb.Headers.Add("Disposition-Notification-To", sender)
        End If
        mailWebb.SubjectEncoding = System.Text.Encoding.UTF8
        mailWebb.BodyEncoding = System.Text.Encoding.UTF8
        mailWebb.Subject = TxtBoxSubject.Text.Trim
        mailWebb.Body = TxtBlock.Text.Trim
        mailWebb.BodyEncoding = System.Text.Encoding.UTF8
        mailWebb.IsBodyHtml = False
        If UpFile.HasFile And (SparadFil = True) Then
            Dim fileName As String
            For Each postedFile As HttpPostedFile In UpFile.PostedFiles
                fileName = Path.GetFileName(postedFile.FileName)
                mailWebb.Attachments.Add(New Net.Mail.Attachment(Path.Combine(SavePath, fileName)))
            Next
        End If
        Try
            SmtpServer.Send(mailWebb)
            'sudda och nollställ
            Response.Write("<script language='javascript'> alert('Meddelandet är skickat'); </script>")
        Catch error_t As SmtpException
            Response.Write("<script language='javascript'> alert('Meddelandet kunde inte skickas');</script>")
        End Try
    End Using
End Sub

At present - in the web.config following code:

For security I want to move the sensitive information from code behind to the web.config file but I can't figure out how, despite extensive web-search.

Grateful if someone can provide a link where I can find a solution or give an example for changing the web.config file. Best regards and thanks. /KurtJ

Found some help and tried to implement - but as usually - got error(s) This time from IIS: The configuration section 'smtp_info' cannot be read because it is missing a section declaration. In VS2019, i have the first line <smtp_info deliveryMethod="Network" from="info@myWeb.se" > green underlined and saying : The element 'MailSettings' has invalid child element 'smtp_info' So - what have I missed - or what is wrong? Hopefully someone can help. DS.

The new web.cfg (partly)

<configruration>
  <configSections >
    <sectionGroup name ="mailSettings">
      <section name ="smtp_info" type ="System.Net.Configuration.SmtpSection" />
      <section name ="smtp_webmaster" type ="System.Net.Configuration.SmtpSection"/>
      <section name ="smtp_kontroll" type ="System.Net.Configuration.SmtpSection"/>
      <section name ="smtp_admin" type ="System.Net.Configuration.SmtpSection"/>
    </sectionGroup>
  </configSections>
  <system.net >
    <mailSettings>
      <smtp_info deliveryMethod="Network" from="info@myWeb.se" >
        <network host="mySmtpHost.com" port="587" enableSsl="true" userName="info@myWeb.se"  password="pwd_info"  />
      </smtp_info>
      <smtp_kontroll deliveryMethod="Network" from="kontroll@myWeb.se">
        <network host="mySmtpHost.com" port="587" enableSsl="true" userName="kontroll@myWeb.se" password="pwd_kontroll"  />
      </smtp_kontroll>
      <smtp_admin deliveryMethod="Network" from="admin@myWeb.se">
        <network host="mySmtpHost.com" port="587" enableSsl="true" userName="admin@myWeb.se" password="pwd_admin"  />
      </smtp_admin>
      <smtp_webmaster deliveryMethod="Network" from="webmaster@myWeb.se">
        <network host="mySmtpHost.com" port="587" enableSsl="true" userName="webmaster@myWeb.se" password="pwd_webmaster"  />
      </smtp_webmaster>    
    </mailSettings>
  </system.net>
</configuration>

and aspx page

Dim SmtpServer As New Net.Mail.SmtpClient
Dim section As SmtpSection
Using SmtpServer
  SmtpServer.UseDefaultCredentials = False
  Select Case ddListAvs.SelectedValue
    Case Is = "info@myWeb.se"
       section = CType(ConfigurationManager.GetSection("mailSettings/" & "smtp_info"), SmtpSection)
       SmtpServer.Credentials = New NetworkCredential(section.Network.UserName, section.Network.Password, section.Network.ClientDomain)
       SmtpServer.EnableSsl = section.Network.EnableSsl
       SmtpServer.Host = section.Network.Host
       SmtpServer.Port = section.Network.Port
       MsgBox("Host= " + SmtpServer.Host)
  end select
  end using
  exit sub
  • have a look to this similat topic : https://stackoverflow.com/questions/2260317/change-a-web-config-programmatically-with-c-sharp-net – tuyau2poil Mar 04 '23 at 12:01
  • You might find [Protecting Connection Strings and Other Configuration Information (C#)](https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/advanced-data-access-scenarios/protecting-connection-strings-and-other-configuration-information-cs) to be of use. Even though it's in C#, it should not be too difficult to translate it to VB.NET. – Andrew Morton Mar 04 '23 at 16:55
  • Thanks for efforts to help, but not exactly what I am looking for. I have edited my question. – Kurt Johansson Mar 06 '23 at 11:44
  • You could add an appSettings section to your web.config file, and within that add as many key/value pairs as needed - eg add key="whatever" value="something" - then you canr ead these as requried from code behind using ConfigurationManager.AppSettings("[key name]") – PSU Mar 12 '23 at 11:19

0 Answers0