简体   繁体   English

SmtpClient.SendAsync不发送电子邮件而不是SmtpClient.Send

[英]SmtpClient.SendAsync not sending email versus SmtpClient.Send

I am working on a project which (unfortunately) requires the maximum framework allowable to be .NET 4. 我正在开发一个项目(不幸的是)需要允许最大框架为.NET 4。

In this project I am attempting to send an email asynchronously using SendAsync() but for some reason, it fails to send (or rather, it just doesn't send). 在这个项目中,我试图使用SendAsync()异步发送电子邮件但由于某种原因,它无法发送(或者更确切地说,它不发送)。 Simply put, nothing happens. 简单地说,没有任何反应。 No error or anything, whats strange is that when I set a breakpoint at the event handler code, and step through the program, it doesn't go into the event handler at any point. 没有错误或任何事情,有些奇怪的是,当我在事件处理程序代码中设置断点并逐步执行程序时,它不会在任何时候进入事件处理程序。

If I use the synchronous method Send(); 如果我使用同步方法Send(); it works flawlessly. 它完美无瑕。

The only difference in my code is the using an event handler for the SendAsync(); 我的代码唯一的区别是使用SendAsync();的事件处理程序SendAsync(); method. 方法。 ( SendCompleteEventHandler ) SendCompleteEventHandler

below is the code I have tried so far to send an email. 下面是我到目前为止发送电子邮件的代码。

private static void SendMailAsync()
{

    const string FROM = "testmail@testmail.com";
    const string TO = "recipient@testmail.com";

    string SUBJECT = "THIS IS A TEST";
    string BODY = "THIS IS A TEST";


    const string SMTP_USERNAME = "UserName";  //SMTP username. 
    const string SMTP_PASSWORD = "Password";  // SMTP password.


    const string HOST = "host.testsmtp.com";
    const int PORT = 2587;

    // Create an SMTP client
    SmtpClient client = new SmtpClient(HOST, PORT);
    client.SendCompleted += new SendCompletedEventHandler(Client_SendComplete);

    // Create a network credential
    client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
    client.EnableSsl = true;

    string userToken = "testmsg1";

    // Send the email. 

        client.SendAsync(FROM, TO, SUBJECT, BODY, userToken);

    client.Dispose();

}

private static void Client_SendComplete(object sender, AsyncCompletedEventArgs e)
{
    string token = (string) e.UserState;

    if (e.Error != null)
    {
        MessageBox.Show(e.Error.ToString());
    }
    else
    {
        MessageBox.Show("Message sent.");
    }
}

private void button2_Click(object sender, EventArgs e)
{
    SendMailAsync();
}

Is there something I am just flat out missing? 有什么东西我只是缺了吗? Thanks. 谢谢。

The operation called asynchronous if it's being run asynchronously , in background. 如果它在后台asynchronously运行,则该操作称为asynchronous So, you should consider the SendAsync method as start for the operation. 因此,您应该将SendAsync方法视为操作的开始 After this, you're disposing the client which actually run the operation, without waiting for it to complete, so you're, like, saying: hey, start this for me, please... Oh, nevermind. 在此之后,你正在处理实际运行操作的客户端,而不是等待它完成,所以你就像说:嘿,请为我开始,请...哦,没关系。

So, you probably should remove the Dispose method from the code right after the sending. 因此,您可能应该在发送后立即从代码中删除Dispose方法。 You can dispose your client in Client_SendComplete ( sender object probably is a client), or you can make a static field in your class, and dispose it after your program is shut down (as far as I can see, it's a WinForms project so you can handle some windows events) 你可以在Client_SendComplete处理你的客户端( sender对象可能是客户端),或者你可以在你的类中创建一个静态字段,并在你的程序关闭后处理它(据我所见,它是一个WinForms项目,所以你可以处理一些Windows事件)

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

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