0
votes
MailMessage message = new MailMessage();

message.From = new MailAddress("MyMailAddress");
message.To.Add("DestinationMailAddress");
message.CC.Add("CCMailAddress");
message.Subject = "This is Subject";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = "This is a test e-mail message sent by an application. ";


SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Send(message);

This my code. The error was

Failure sending email.

Inner Exception:

{"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.68.109:587"} Errorcode:10060

3
what's in your stacktrace?woodykiddy
{"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.68.109:587"} Errorcode:10060Kenz
Give it a go with user1666620 suggestion. I'd say it's got something to do with the credential as well.woodykiddy

3 Answers

4
votes

Your code uses gmail as your SMTP:

client.Host = "smtp.gmail.com";

Yet you specify your SMTP login credentials to be your windows account:

client.UseDefaultCredentials = true;

If gmail is in fact your SMTP, you need to set this to false and then provide your gmail login credentials.

So your code should look like:

client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");

It also looks like you might have a firewall issue as well

{"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.68.109:587"} Errorcode:10060

Make sure that the application has access to the internet and the necessary ports are open to reach gmail. Also make sure that on your gmail account, less ssecure applications have been allowed, and that, if you are using 2-factor authentication, that you have created an application-specific password for the account and are using that to connect.

How to enable less secure apps: https://support.google.com/accounts/answer/6010255?hl=en

You might also need to change how your MailAddress.To is populated:

message.To.Add(new MailAddress("DestinationMailAddress"));
0
votes

Since the functionality is wrapped you should use the inner exception message to get the detailed error message. The failure reason will be right there!

public static void Main()
{
   try {
       SendMail();
  }
  catch(Exception e) {
     if (e.InnerException != null)
        Console.WriteLine("Inner exception: {0}", e.InnerException);
  }

}

0
votes

Make these changes

client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");

And this too

http://docs.helpscout.net/article/120-smtp-settings

Full working code

protected void SendMail()
        {
            MailMessage msg = new MailMessage();
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            try
            {
                msg.Subject = "Add Subject";
                msg.Body = "Add Email Body Part";
                msg.From = new MailAddress("Valid Email Address");
                msg.To.Add("Valid Email Address");
                msg.IsBodyHtml = true;
                client.Host = "smtp.gmail.com";
                System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("Valid Email Address", "Password");
                client.Port = int.Parse("587");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = basicauthenticationinfo;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(msg);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }