使用 C# 发送电子邮件的问题

Email sending issue using C#

我正在使用 c# 发送电子邮件,并从 web.config 文件中使用的 appsettings 获取所有值,但我的问题是我没有收到任何电子邮件,尽管我没有收到任何异常或错误。请帮我解决我的问题这是我的代码

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;
        }
    }

这是我的 web.config 设置

 <add key="smtpServer" value="smtp.gmail.com" />
<add key="smtpPort" value="587" />
<add key="smtpUser" value="offey2018@gmail.com" />
<add key="smtpPass" value="*******" />

您不是在等待它完成发送电子邮件,而是在发送电子邮件之前处理(using 块)SmtpClient

smtp.SendMailAsync(mailMessage);

Async returns a Task 如果您想等到它完成,则必须等待它。

示例:

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

或者,也可以使用这个:

smtp.Send(mailMessage);

试试这个

如果您打算使用 SendMailAsync 方法,您可以按照以下步骤操作

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;
    }
}

否则您可以使用以下方式通过非异步方法发送邮件

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;
    }
}

备注

async 方法只能有两种 return 类型 1) Void 2)Task 。因此,您可以根据需要使用任何一种方式。

希望对您有所帮助。如果您有任何疑问或问题,请告诉我。