0

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

  1. Smtp status code General Failure
  2. 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?

Prashant Bhanarkar
  • 930
  • 3
  • 14
  • 32

3 Answers3

0

Here is the code for sending mail programmatically using c#. it is working fine in my machine.

    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

        mail.From = new MailAddress("email");
        mail.To.Add("email");
        mail.Subject = "Subject";
        mail.Body = "Message";
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("email", "password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);
        return true;
    }
    catch (Exception ex)
    {
        return "false";
    }
JeetDaloneboy
  • 399
  • 2
  • 16
0
   MailMessage mail = new MailMessage();
   mail.From = new MailAddress("frommail", "fromText");
   mail.To.Add(toEMailId);
   mail.Subject = "TestSubject";
   mail.IsBodyHtml = true;
   mail.BodyEncoding = System.Text.Encoding.UTF8;
   mail.Body = "Test mailBody";
   //SMTPServer
   SmtpClient SmtpServer = new SmtpClient("yourSMTPServer");
   //SMTPPort
   SmtpServer.Port = Convert.ToInt32("PostNumber");
   SmtpServer.UseDefaultCredentials = false;
   //Set SMTP Username and Password
   SmtpServer.Credentials = new             System.Net.NetworkCredential("SMTPUserName","SMTPPassword");
  //EnableSSL
  SmtpServer.EnableSsl = false;
  SmtpServer.Send(mail);
Hitesh Thakor
  • 471
  • 2
  • 12
0

Here is my code, you can try.

public static bool SendEmail(string subject, MailAddress from, List<string> to,List<string> cc, string body, Attachment attachment)
{
    try
    {

        MailMessage mail = new MailMessage();
        mail.Subject = subject;
        mail.From = from;
        foreach (var item in to)
            mail.To.Add(item);
        foreach (var item in cc)
            mail.CC.Add(item);
        mail.IsBodyHtml = true;
        mail.Body = body;
        if (attachment != null)
            mail.Attachments.Add(attachment);

        SmtpClient mailClient = new SmtpClient();
        mailClient.Port = 587;//maybe 25
        mailClient.Host = "smtp.gmail.com";
        mailClient.EnableSsl = true;
        mailClient.Credentials = new NetworkCredential("EmailAddress", "EmailPassword");
        //mailClient.Credentials = new NetworkCredential("a@gmail.com", "1234");


        try
        {
            mailClient.Send(mail);
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }
    catch { return false; }
}