简体   繁体   English

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

[英]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. 我正在使用c#发送电子邮件,并从web.config文件中使用的appsettings获取所有值,但我的问题是尽管没有异常或错误,但我没有收到任何电子邮件。 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 这是我的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="*******" />

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: 您不是在等待它完成发送电子邮件,而是在发送电子邮件之前( using块)处置SmtpClient

smtp.SendMailAsync(mailMessage);

Async returns a Task which you have to await if you want to wait until it's completed. 异步返回一个要等待完成的Task ,您必须等待它。

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

Try this 尝试这个

If you are planning to use SendMailAsync method you can follow the below 如果您打算使用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;
    }
}

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 . 异步方法只能有两种返回类型1)无效2)任务。 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. 如果您有任何疑问或问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM