1

I am sending an email using c# and get all the values from appsettings used in web.config file but my problem is that i didn't receive any email though there is no exception or error i am getting. Please help me resolve my issue here is my code

public static bool SendMail(string to,string subject, string body)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
            mailMessage.To.Add(new MailAddress(to));
            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;
            using (SmtpClient smtp = new SmtpClient())
            {
                smtp.EnableSsl = true;
                smtp.Host = ConfigurationManager.AppSettings["smtpServer"];
                smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.SendMailAsync(mailMessage);
                return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

here is my web.config setting

 <add key="smtpServer" value="smtp.gmail.com" />
<add key="smtpPort" value="587" />
<add key="smtpUser" value="offey2018@gmail.com" />
<add key="smtpPass" value="*******" />
habib
  • 2,366
  • 5
  • 25
  • 41
Shamshad Jamal
  • 19
  • 3
  • 17

2 Answers2

3

You're not waiting for it to finish sending the e-mail and you're disposing (using block) of the SmtpClient before the email can be sent:

smtp.SendMailAsync(mailMessage);

Async returns a Task which you have to await if you want to wait until it's completed.

Example:

// this does require that the enclosing method is also async, and awaited
await smtp.SendMailAsync(mailMessage);

Or, alternatively, just use this:

smtp.Send(mailMessage);
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
2

Try this

If you are planning to use SendMailAsync method you can follow the below

public static async Task SendMail(string to,string subject, string body)
{
    try
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
        mailMessage.To.Add(new MailAddress(to));
        mailMessage.Subject = subject;
        mailMessage.IsBodyHtml = true;
        mailMessage.Body = body;
        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.EnableSsl = true;
            smtp.Host = ConfigurationManager.AppSettings["smtpServer"];
            smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            await smtp.SendMailAsync(mailMessage);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

or else you can use below way to send mail by using non async method

public static bool SendMail(string to,string subject, string body)
{
    try
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
        mailMessage.To.Add(new MailAddress(to));
        mailMessage.Subject = subject;
        mailMessage.IsBodyHtml = true;
        mailMessage.Body = body;
        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.EnableSsl = true;
            smtp.Host = ConfigurationManager.AppSettings["smtpServer"];
            smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(mailMessage);
            return true;
        }
    }
    catch (Exception ex)
    {
        return false;
    }
}

Note

async method can only have two return types 1) Void 2)Task . Therefore you can use either way according to your need.

I hope this will help you. If you have any doubts or issues let me know.

Suvethan Nantha
  • 2,404
  • 16
  • 28
  • 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... I am getting following error – Shamshad Jamal Aug 01 '18 at 06:29
  • @ShamshadJamal did you check the below article I think it's because of the timezone you can fix it by following the below article? https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – Suvethan Nantha Aug 01 '18 at 06:36