0

Getting the exception:

Failure sending mail.

While using System.Net.Mail.Smtp in C#.NET on the line smtp.Send(message);

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();            
message.To.Add(sendto);    
message.Subject = "CP1 Lab Password";
message.From = new System.Net.Mail.MailAddress("abc@gmail.com");
message.Body = mail_message;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");

smtp.Host = "smtp.gmail.com";
smtp.Port = 465; //(465 for SSL)587 
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "mypassword");    
smtp.Send(message);

Edit 1:

Here is the detail of the error:

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 ...

The same error when using port 25.

Few months before this code used to work but today it is not working

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
user8109
  • 189
  • 1
  • 12
  • Have a look at [How to use gmail SMTP in ASP.NET form](http://stackoverflow.com/questions/7982810/how-to-use-gmail-smtp-in-asp-net-form/7982891#7982891) – huMpty duMpty Aug 19 '15 at 11:02

1 Answers1

0

As from your exception message (incomplete) and the code, it is very hard to say what went wrong in your case. Failure sending mail is thrown when the server is not found, port cannot be connected. It means that connection was not established, until now.

The exception message that gets raised if the connection was established but authentication was not performed correctly is, "The SMTP server requires an authenticated connection...". I would suggest that you check the PORT, SMTP server host (do not add http://) and then re-try. An SMTP connection (in most general cases) requires

  1. SMTP Server host: smtp.gmail.com is enough!
  2. PORT to connect at: I have always used default TCP port for SMTP, 25. It works.
  3. EnableSsl: most require it. I suggest you always use it. client.EnableSsl = true;
  4. Credentials are required by all: No server would allow bots sending emails. In this concern, if you create a new account. You may face trouble sending emails programmatically, I faced the problem with a new account not sending the emails whereas an old account (default account of mine) was sending the email without any trouble.

The following code template (if filled with accurate parameters) will definitely send the email, because I have tested and verified it bazillion times. :)

// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
   // Configure the client
   client.EnableSsl = true;
   client.Credentials = new NetworkCredential("<username>", "<password>");
   // client.UseDefaultCredentials = true;

   // A client has been created, now you need to create a MailMessage object
   MailMessage message = new MailMessage(
                            "from@example.com", // From field
                            "to@example.com", // Recipient field
                            "Hello", // Subject of the email message
                            "World!" // Email message body
                         );

   // Send the message
   client.Send(message);

   /* 
    * Since I was using Console app, that is why I am able to use the Console
    * object, your framework would have different ones. 
    * There is actually no need for these following lines, you can ignore them
    * if you want to. SMTP protocol would still send the email of yours. */

   // Print a notification message
   Console.WriteLine("Email has been sent.");
   // Just for the sake of pausing the application
   Console.Read();
}

Sending an email may be a headache sometimes, because it requires some basic understanding of networking also. I have written an article that covers sending emails in .NET framework and a few problems that beginners usually stumble upon such as this one. You may be interested in reading that article also. http://www.codeproject.com/Articles/873250/Sending-emails-over-NET-framework-and-general-prob

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • Yes I did. Except for point 2, I was following all points. After your post, I tried port 25 also. But did not work. I checked "allow to less secure app" also and removed the "yoursmtphost" word as suggested by user2946329. But it did not work. Only thing remaining is to copy paste the code given by you. Can you point out where is the mistake? – user8109 Aug 19 '15 at 15:00
  • It is unclear as to what may be the problem, can you update your question and post your own code? **You can surely hide the password**. Post the rest of the code. – Afzaal Ahmad Zeeshan Aug 19 '15 at 15:02
  • Make sure you can actually ping the server and establish a connection, i was having issues where the network administrators have blocked access to specific ports. – Marko Aug 19 '15 at 17:43
  • "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.200.109:25 " That is the error. When I tried to execute **ping 74.125.200.109** using cmd, request timed out. – user8109 Aug 21 '15 at 06:04