Error trying to send email
Apologies as I'm a bit of a beginner with this. I'm trying to send an email from my ASP.NET website using the following code (obviously replacing the hostname, username and password with the actual values):
Public Shared Sub Send(ByVal ToEmail As String, ByVal FromEmail As String, ByVal Subject As String, ByVal Message As String)
Dim mm As New MailMessage(FromEmail, ToEmail, Subject, Message)
Dim smtp As开发者_运维百科 New SmtpClient("hostname")
smtp.Credentials = New NetworkCredential("username", "password")
smtp.Send(mm)
End Sub
When trying to send out the mail I'm receiving this error:
"Unable to read data from the transport connection: net_io_connectionclosed."
I've had a look on various forums to try to find some help with this but I'm not really sure what I should be doing to recify this.
Any help would be very much appreciated. Thanks.
Thanks to everyone for their responses. I actually managed to resolve this in the end by contacting Namesco - they said all I need to do is change the hostname that I had put in to "localhost" and it's worked.
You haven't set your SMTP server in your web.config.
See the web.config in the article Sending Email with System.Net.Mail
You can do this in IIS, but I would recommend setting it in the application's web.config for easier access by the development team.
Basically your SMTP server is blocking your request to send mail.
You can easily test this yourself by opening a command window and use the telnet command to try and connect to the mail server - it will kick you out immediately.
I haven't used Namesco before, but this problem was resolved by adding the IP address of the server trying to send the mail to the list of allowed machines in IIS.
I'm guessing Namesco won't change the setting to "All Unassigned" as that would grant anyone permission to send mail.
ADD the below lines to your web.config file
<system.net>
<mailSettings>
<smtp from="email@yourdomain.com">
<network host="smtp.yourdomain.com" port="25" userName="email@yourdomain.com" password="XXXXX" />
</smtp>
</mailSettings>
</system.net>
Below is the code to send mail hope this helps
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress("somebody@yahoo.com")
If Not String.IsNullOrEmpty("somebody@yahoo.com") Then
mMailMessage.ReplyTo = New MailAddress("somebody@yahoo.com")
End If
mMailMessage.To.Add(New MailAddress("somebody@yahoo.com"))
mMailMessage.Bcc.Add(New MailAddress("somebody@yahoo.com"))
mMailMessage.CC.Add(New MailAddress("somebody@yahoo.com"))
mMailMessage.Subject = "some subject"
mMailMessage.Body = "some body"
mMailMessage.IsBodyHtml = False
mMailMessage.Priority = MailPriority.Normal
Try
Dim mSmtpClient As New SmtpClient()
mSmtpClient.Send(mMailMessage)
Catch ex As SmtpException
Debug.Print("Unable to send message " & ex.Message)
End Try
精彩评论