2

I tried to send mail using Windows application in C#. I got code as below and tried but it is not working

It giving following error:

Unable to connect the remove server

Code:

MailAddress sendd = new  MailAddress("xxxxxx@gmail.com","Sethu",Encoding.UTF8);
MailAddress receivee = new MailAddress("xxxxxx@yahoo.com");
MailMessage mail = new MailMessage(sendd, receivee);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xxxxxx@gmail.com", "yyyyyyy");
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

Anyone help to find out what mistake i did?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Sethu Raman
  • 143
  • 2
  • 13

2 Answers2

0

can you try make your code to something like the following

 var smptClient = new SmtpClient("smtp.gmail.com", 587)
 {
     Credentials = new NetworkCredential("smit123@gmail.com", "123456789"),
     EnableSsl = true
 };
  smptClient.Send("smit123@gmail.com", "test@gmail.com", "Testing Email", "testing the email");

This code is properly tested with my own account on the Network..

Smit Patel
  • 2,992
  • 1
  • 26
  • 44
  • Everything is correct in code. Is there any issue with rights or need to change any smtp settings. – Sethu Raman Jun 23 '16 at 10:17
  • So may be the issue is in your `NetworkCredentials`, Please check those configuration first, as your code is working perfect – Smit Patel Jun 23 '16 at 12:13
0

Following code works for me

var client = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential("xxxxxx@gmail.com", "yyyyyyy"),
    EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "this is a test email.", "this is my test email body");

which is pretty much the same as yours with the difference that it doesn't use MailMessage object. This might not be the problem, but you could give it a try.

I would also check if you have port 587 enable on your server

You should also go through this topic where you find lots of useful tips

Community
  • 1
  • 1
Václav Holuša
  • 311
  • 3
  • 14