Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi, i am trying to send mail using smtp.gmail in C# win app. it gives me error

Unable to connect to the remote server.
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 74.125.127.109:587

What could be wrong with following code. Any help would be greatly appreciated


System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage();
MyMailMessage.From = new System.Net.Mail.MailAddress("username@gmail.com");
MyMailMessage.To.Add("username@gmail.com");
MyMailMessage.Subject = "Feedback Form";
MyMailMessage.Body = "this is test";
System.Net.Mail.SmtpClient SMTPServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
SMTPServer.Port = 587;
SMTPServer.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
SMTPServer.EnableSsl = true;
SMTPServer.Send(MyMailMessage);
Posted

check whether the port number for the domain(gmail.com) is correct or not.
 
Share this answer
 
Hi,

Sorry for the mistake... :)


Sending Emails from C# Application using default SMTP[^]

Check this one...

Thanks
 
Share this answer
 
Google! A quick google[^] using "send mail using smtp.gmail in C#" copied and pasted from your question found over 100,000 hits, including: Send Email from your GMAIL account using ASP.Net and C#[^] - which includes full source code.

Learn to do at least basic research for yourself!
 
Share this answer
 
Hi,

The problem is that in Credentials you are using username@gmail, here you should use only username :)

this code is working fine

MailMessage mail = new MailMessage();
               SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
               mail.From = new MailAddress("xyz@gmail.com");
               mail.To.Add("abc@gmail.com");
               mail.Subject = "Test Mail";
               mail.Body = "test mail";
               SmtpServer.Port = 587;
               SmtpServer.Credentials = new System.Net.NetworkCredential("abc", "password");
               SmtpServer.EnableSsl = true;
               SmtpServer.Send(mail);
              Response.Write("mail Send");





Rate the solution if you like
 
Share this answer
 
Comments
Member 4230610 19-Jul-11 5:35am    
hi, thanks for your inputs. but i am still getting error "Unable to connect to the remote server"
Specifying a port in a .NET medium trust environment usually ends up in a security exception, and IIRC, you can use port 25 with Gmail -- even with EnableSsl set to true.
 
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