I wrote the following piece of code for sending an email. First, I wanted to try it with gmail.
Code:
public class EmailSender
{
public static void SendEmail(
string from,
string to,
string smtpaddress, int port,
string username,
string password,
string title,
string body)
{
try
{
SmtpClient client = new SmtpClient(smtpaddress, port);
client.Credentials = new System.Net.NetworkCredential(username, password);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = title;
mail.Body = body;
mail.IsBodyHtml = true;
client.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Using this is pretty simple:
Code:
class Program
{
public static void Main(string[] args)
{
EmailSender.SendEmail(
"john.doe@gmail.com",
"jane.doe@gmail.com",
"smtp.gmail.com",
465,
"john.doe",
"password",
"test",
"<p>This is a test email!</p>");
}
}
This doesn't work. I get a SmtpException, "The operation has timed out."
I also tried it like this:
Code:
class Program
{
public static void Main(string[] args)
{
EmailSender.SendEmail(
"<john.doe@gmail.com>",
"<jane.doe@gmail.com>",
"smtp.gmail.com",
465,
"john.doe",
"password",
"test",
"<p>This is a test email!</p>");
}
}
Same problem. If I change the port to 587 (http://mail.google.com/support/bin/a...n&answer=13287), I get the following exception:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Anyone has suggestions what could be wrong?
Thank you.