Click here to Skip to main content
15,913,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used the following code but it shows that the system.net.mail failed sending email . It also shows that the connection host failed to respond. So, please can anyone tell me what is the problem. I am using LAN connection of my school and i haven't configured any email client. I have also tried changing ports but it still doesnot work.
VB
Try
  Dim Smtp_Server As New SmtpClient
  Dim e_mail As New MailMessage()
  Smtp_Server.UseDefaultCredentials = False
  Smtp_Server.Credentials = New Net.NetworkCredential("username@gmail.com", "password")
  Smtp_Server.Port = 587
  Smtp_Server.EnableSsl = True
  Smtp_Server.Host = "smtp.gmail.com"

  e_mail = New MailMessage()
  e_mail.From = New MailAddress(txtFrom.Text)
  e_mail.To.Add(txtTo.Text)
  e_mail.Subject = "Email Sending"
  e_mail.IsBodyHtml = False
  e_mail.Body = txtMessage.Text
  Smtp_Server.Send(e_mail)
  MsgBox("Mail Sent")

Catch error_t As Exception
  MsgBox(error_t.ToString)
End Try
Posted
Updated 17-Mar-14 4:52am
v2
Comments
Richard MacCutchan 17-Mar-14 10:51am    
The details above look correct. Is gmail blocked by your school firewall by any chance?
joshrduncan2012 17-Mar-14 10:56am    
Isn't there an "Inner Exception" that the OP could display that could possibly explain a little further why this is failing?
Richard MacCutchan 17-Mar-14 11:39am    
Probably, the Exception class has all sorts of good stuff in it.

1 solution

Hi, the cause is most probably because you did not enable the SSL with gmail. I suggest that you add:

VB
Try
  Dim Smtp_Server As New SmtpClient
  Dim e_mail As New MailMessage()
  Smtp_Server.UseDefaultCredentials = False
  Smtp_Server.Credentials = New Net.NetworkCredential("username@gmail.com", "password")
  Smtp_Server.Port = 587
  Smtp_Server.EnableSsl = True
  Smtp_Server.Host = "smtp.gmail.com"
  SmtpServer.EnableSsl = True


SmtpServer.EnableSsl = True to your code. Also Gmails sometimes creates problems when sending emails trough this manner, so after you test your solution, please login to your gmail and check the security issues (they will most probably ask you about that transaction and you might need to enter a captcha on their login page to validate that you are the one using it). Also, check that SMTP is enabled in Gmail.

Also do check the How to Send Mails from your GMAIL Account through VB.NET or C#. Windows Programming, with a Bit of Customization[^] tutorial from CodeProject.
 
Share this answer
 
v2
Comments
Richard MacCutchan 17-Mar-14 11:40am    
I just tried the exact code as posted by the OP (with my username) and it works fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900