Last post Oct 08, 2008 02:06 PM by Flintronics
Oct 07, 2008 02:27 AM|Flintronics|LINK
I am writing a small program that connects to a SMTP server to send emails. I use the system.net.mail class so mailmessage, smtpclient, etc. When i try to connect to the smtp server let's say xxx.test.com, port 465, i get socket errors:
Send Email Failed.System.Net.Mail.SmtpException:
Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection
was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte buffer,
Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte
buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.SmtpConnection.GetConnection(String
host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage
message) at _Default.EmailButton_Click(Object sender, EventArgs e) in
I used the same code to connect to smtp.gmail.com, port 587 and it worked find so i can eliminate the possibility of my code being wrong. My question is which ports is the system.net.mail class using to connect to the SMTP server such as google's? Does it
use http or port 25?
Also i have searched for causes of the above error on google and get a lot of hits but no clear solutions. Thank you for your help.
Oct 07, 2008 03:11 AM|AMR_PHASE|LINK
It uses port 25 unless you specify otherwise...
Oct 07, 2008 08:07 PM|Flintronics|LINK
That is for setting the port value on the remote computer. I was being bit stupid with that question so ignore that. :D Here's what i should have asked:
I can connect to the smtp server using outlook client on my computer but when i use the application i get the error that i mentioned in the first post. Here's my code:
Dim Message As New MailMessage
Message.From = New MailAddress("myEmail")
Message.Subject = "Work, Please!"
Message.Body = "Body test"
Message.IsBodyHtml = True
Dim smtp As New SmtpClient
smtp.EnableSsl = True
smtp.Host = "smtp server address"
smtp.Port = XX
smtp.UseDefaultCredentials = False
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
smtp.Credentials = New System.Net.NetworkCredential("username", "password")
Catch ex As Exception
Response.Write("Send Email Failed." + ex.ToString())
Oct 07, 2008 08:27 PM|Flintronics|LINK
Just one more thing to add:
When i use Outlook to connect to google's smtp, i select the option "Enable SSL" so port 465 and it works.
In my application however, when i say "smtp.EnableSsl = true" and set port to 465, it doesn't work, i have to change the port to 587 for it to work. I am not sure why this is but it seems like the statement "smtp.EnableSsl = true" is not doing it's job.
Oct 07, 2008 09:14 PM|AdvanIntel|LINK
Oct 07, 2008 09:16 PM|AdvanIntel|LINK
Oct 08, 2008 02:06 PM|Flintronics|LINK
Thanks Dave but i already tried to Telnet and it worked fine. Also, as far as service running on it, the whole reason for that system is to act as a smtp server and i could also connect to it using outlook and send mail so we can rule that out.
I am now wondering if it is actually something to do with EnableSsl property. When i have that as "true" and try to connect to google using port 465, it doesn't work but the same properties work if i use Outlook as a client. Just like the problem above. :(