Click here to Skip to main content
15,943,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to send an email using my gmail account i keep getting an error message failing to send message

public static void SendEmployeeMail(string server)
            MailAddress from = new MailAddress("");
            MailAddress to = new MailAddress("");
            MailMessage message = new MailMessage(from, to);
            message.Subject = "Using the SmtpClient class.";
            message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
            SmtpClient smtp = new SmtpClient(server);
            smtp.Host = "";
            smtp.Port = 587;
            smtp.Credentials = new NetworkCredential("", "mypassword");
             smtp.EnableSsl = true;
            Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1} port {2}.",
            to.ToString(), smtp.Host, smtp.Port);
Updated 5-Jan-16 4:46am
ZurdoDev 5-Jan-16 10:41am    
And the error is?
Member 11925473 5-Jan-16 10:43am    
Failure sending message
Suvendu Shekhar Giri 5-Jan-16 10:52am    
Share the complete error message.

The error message is not enough, however "Failure sending mail." means that .NET was not able to complete the request. Typically, either because it was not able to connect to the server and there are many possibilities for this:

1. Hostname is not correct.
2. Port not available; try 25 which is default TCP port for SMTP communication.
3. Firewall or Antivirus in action.

The code that you are using "should" work. Check if there are any troubles connecting to the server. Also, use 25 port! It "always" works in my case. The typical code for sending the email (assuming no connection or authentication error occurs)

// 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 field
                            "", // Recipient field
                            "Hello", // Subject of the email message
                            "World!" // Email message body

   // Send the 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

Also read this article of mine, that covers these sections: Sending emails over .NET framework, and general problems – using C# code[^]
Share this answer
Sergey Alexandrovich Kryukov 5-Jan-16 14:15pm    
Happy New Year!
Afzaal Ahmad Zeeshan 6-Jan-16 7:41am    
Thank you, Sergey!

Happy New Year to you too! :-)
Member 11925473 6-Jan-16 1:09am    
Still the same problem
Afzaal Ahmad Zeeshan 6-Jan-16 7:42am    
Please re-check the connection problems. The problem is with the connection being established. As mentioned in Solution 2, allow your account to be used by applications for SMTP purposes.

The thing is, it is up to Google to allow your application to use the service or not. In that case, .NET tells you it failed sending the mail.
Member 11925473 6-Jan-16 7:51am    
How do I check them? because the problem is it unable to connect to server. How do I fix this problem?
I had a similar problem recently, there are few rules that Google has that can cause this problem.

The first step is to allow less secure applications to use your Gmail account. It can be allowed in your account settings, you'll find more info about it here:

Allowing less secure apps to access your account - Accounts Help[^]

The second part is, that even when you allow this, Google will refuse to send email (from less secure app) on new computer, where you have never logged into your account from. For example, when you move your application to server. In this case, just log in on and it should work from that moment.
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