Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MailMessage mail = new MailMessage();
     mail.From = new MailAddress("from@gmail.com");
     mail.To.Add("to@gmail.com");
     mail.Subject = "Hello World";
     mail.Body = "<h1>Hello</h1>";
     mail.IsBodyHtml = true;
     mail.Attachments.Add(new Attachment("C:\\report.txt"));

     using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
     {
         smtp.Credentials = new System.Net.NetworkCredential("from@gmail.com", "password");
         smtp.EnableSsl = true;
         smtp.Send(mail);
         MessageBox.Show("Message sent");
     }


What I have tried:

Above code worked fine in my own laptop, but when I used it in networked PC (as a simple user, not admin),
I faced this error
ystem.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 64.233.162.109:587
Posted
Updated 26-Jan-17 23:10pm
Comments
F-ES Sitecore 27-Jan-17 5:52am    
This is a network issue, maybe that port is blocked on your firewall. Google the error message for possible solutions, sending email through gmail is one of the most frequently asked questions, every problem has already been solved numerous times if you google.

1 solution

Your code looks fine to me, and it should send the email (and of course, should connect to the server), did you try connecting through the default TCP port for SMTP communication? That is 25, consider trying that one out,
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 25))
Other than that, only a few things to consider:

1) Is SMTP enabled in the gmail settings?
2) Is firewall blocking your communication to that IP address/port from your application. This can really matter a lot.
3) Is Google permitting your account to communicate, sometimes I have also found that Google doesn't allow specific applications to use SMTP services.

Other than that, there is very less we can look into. One more thing, kindly try to connect to Outlook or Yahoo! accounts and see if they are working — code looks fine.

Sending emails over .NET framework, and general problems – using C# code[^].
 
Share this answer
 

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