Search code examples
c#asp.net-mvcasync-awaitweb-configsmtpclient

Email sending issue using C#


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="[email protected]" />
<add key="smtpPass" value="*******" />

Solution

  • 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.