Asp.Net 2.0 Net.Mail. I don't know what else to try. Thanks.

S

shapper

Hello,

I am trying to send an email from a form in my web page.

I have the following codes:

...
Dim mailSettings As New
System.Net.Configuration.MailSettingsSectionGroup
Dim smtpClient As New System.Net.Mail.SmtpClient
smtpClient.Host = mailSettings.Smtp.Network.Host
smtpClient.UseDefaultCredentials = False
smtpClient.Port = mailSettings.Smtp.Network.Port
smtpClient.DeliveryMethod =
System.Net.Mail.SmtpDeliveryMethod.Network
smtpClient.Credentials = New
System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName,
mailSettings.Smtp.Network.Password)

smtpClient.Send(message)
...

And in my Web.Config file I have:

<system.net>
<mailSettings>
<smtp deliveryMethod = "network">
<network defaultCredentials = "true"
host = "mail.domain.com"
password = "secret"
port = "25"

userName = "(e-mail address removed)" />
</smtp>
</mailSettings>
</system.net>

I am getting the error:
"ConfigurationSectionGroup cannot be edited before being added to a
section group belonging to an instance of class Configuration"

I looked for a solution all day and I can't solve this.

Could someone tell me what am I doing wrong here?

Thanks,
Miguel
 
S

shapper

Hi,

I solved it this way:

' Create and define the SMTP client
Dim smtpClient As New System.Net.Mail.SmtpClient

' Create access to configuration file
Dim configurationFile As Configuration =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.Config")

' Create mail settings section group
Dim mailSettings As
System.Net.Configuration.MailSettingsSectionGroup =
configurationFile.GetSectionGroup("system.net/mailSettings")

' Define smtp client properties
If Not mailSettings Is Nothing Then
With smtpClient
.Port = mailSettings.Smtp.Network.Port
.Host = mailSettings.Smtp.Network.Host
.Credentials = New
System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName,
mailSettings.Smtp.Network.Password)
End With
End If

Thanks,
Miguel
 
J

Juan T. Llibre

re:

You're quite welcome, Miguel.
Glad to see you're up and running.




Hi,

I solved it this way:

' Create and define the SMTP client
Dim smtpClient As New System.Net.Mail.SmtpClient

' Create access to configuration file
Dim configurationFile As Configuration =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.Config")

' Create mail settings section group
Dim mailSettings As
System.Net.Configuration.MailSettingsSectionGroup =
configurationFile.GetSectionGroup("system.net/mailSettings")

' Define smtp client properties
If Not mailSettings Is Nothing Then
With smtpClient
.Port = mailSettings.Smtp.Network.Port
.Host = mailSettings.Smtp.Network.Host
.Credentials = New
System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName,
mailSettings.Smtp.Network.Password)
End With
End If

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top