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

时间:2018-08-01 05:08:10

标签: c# asp.net-mvc async-await web-config smtpclient

我正在使用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="*******" />

2 个答案:

答案 0 :(得分:3)

您不是在等待电子邮件发送完成,而是在发送电子邮件之前先处置using的{​​{1}}块:

SmtpClient

异步返回smtp.SendMailAsync(mailMessage); ,如果您要等到完成则必须等待。

示例:

Task

或者,也可以仅使用此:

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

答案 1 :(得分:2)

尝试一下

如果您打算使用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;
    }
}

注意

异步方法只能有两种返回类型1)无效2)任务。因此,您可以根据需要使用任何一种方式。

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