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)
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
client.EnableSsl = true;
client.Credentials = new NetworkCredential("<username>", "<password>");
MailMessage message = new MailMessage(
"from@example.com",
"to@example.com",
"Hello",
"World!"
);
client.Send(message);
Console.WriteLine("Email has been sent.");
Console.Read();
}</password></username></smtp-server-address>
Also read this article of mine, that covers these sections:
Sending emails over .NET framework, and general problems – using C# code[
^]