Problém asi je, že z web.configu nevytahujete ani port, ani username, ani heslo... Takhle mi to chodí ve vb.net, do C# si to zkuste přeložit.
Dim configurationFile As Configuration = WebConfigurationManager.OpenWebConfiguration("~\Web.config")
Dim mailSettings As MailSettingsSectionGroup = TryCast(configurationFile.GetSectionGroup("system.net/mailSettings"), MailSettingsSectionGroup)
Dim port As Integer
Dim host As String, password As String, username As String
Dim msgTo As String = TbTo.Text
Dim msgBody As String = TbBody.Text
Dim msgSubj As String = TbSubject.Text
Dim msgFrom As String
If mailSettings IsNot Nothing Then
port = mailSettings.Smtp.Network.Port
host = mailSettings.Smtp.Network.Host
password = mailSettings.Smtp.Network.Password
username = mailSettings.Smtp.Network.UserName
msgFrom = mailSettings.Smtp.From
End If
Dim myMailMessage As New MailMessage(msgFrom, msgTo, msgSubj, msgBody)
Dim SMTPServer As New SmtpClient(host)
With SMTPServer
.Port = port
.UseDefaultCredentials = False
.Credentials = New System.Net.NetworkCredential(username, password)
.EnableSsl = True
End With
Try
SMTPServer.Send(myMailMessage)
Catch ex As SmtpException
lblErr.text = ex.Message
End Try
|