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