I am trying to send email programmatically in C# but I don't find what I am missing. Here is the code for the same
string SendEmail(string toAddress, string subject, string body, string senderID)
{
string result = "Message Sent Successfully..!!";
const string senderPassword = "mypassword";
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
MailMessage message = new MailMessage(senderID, toAddress, subject, body);
smtp.Send(message);
}
catch (SmtpException e)
{
result = "Smtp status code " + e.StatusCode;
}
catch (Exception ex)
{
result ="Error sending email " +ex.ToString();
}
return result;
}
I have tried seeing the value inside result variable its has two value
- Smtp status code General Failure
- The operation has timed out// If we comment the code catch (SmtpException e)
If I look in to the msdn website for SMTP status code "General failure" smtp status code interpretation we will find that the status code means the host cannot be found and I tried doing nslookup and the host "smtp.gmail.com" is available and accessible from telnet.
What I am missing in the program?