c# - SMTP.SendAsync 无法正常工作

标签 c# .net asynchronous smtp task

我正在尝试熟悉 smtp.SendAsync,但由于某种原因,我无法获取邮件消息以异步发送。

这是我尝试过的。

//smtp.SendAsync(mm, null)); Error, Async operation was attempted before another one completed

//Task.Run(() => smtp.SendAsync(mm, null)); No error and no email

//smtp.SendMailAsync(mm));Error, Async operation was attempted before another one completed

// Task.Run(() => smtp.SendMailAsync(mm)); No error and no email.

//smtp.Send(mm); The only one that works, but has that delay and that is what I am attempting to get away from.

我的代码:

public static void Email(IElevation elevation, string fromEmail, string toEmail)
{

    using (Bitmap printCanvas = ShopDrawing.Merger.MergeElevationAndDoor(elevation, RotateFlipType.Rotate90FlipNone))
    {
        using (var ms = new MemoryStream())
        {
            printCanvas.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ms.Position = 0;

            using (MailMessage mm = new MailMessage(new MailAddress(fromEmail), new MailAddress(toEmail)))
            {
                mm.Subject = "[Project: " + elevation.ProjectName + "] " + " Shop drawings for " + elevation.Name;
                mm.Body = "Your shop drawings are attached to this email in reference to Project: " + elevation.ProjectName + " -> Elevation: " + elevation.Name;
                Attachment at = new Attachment(ms, elevation.Name + ".png", "image/png");
                mm.Attachments.Add(at);
                using (SmtpClient smtp = new SmtpClient())
                {
                    //smtp.SendAsync(mm, null));
                    //Task.Run(() => smtp.SendAsync(mm, null));
                    //smtp.SendMailAsync(mm));
                    // Task.Run(() => smtp.SendMailAsync(mm));

                    //The only one that works
                    smtp.Send(mm);

                };
            };
        };
    };
}

最佳答案

将函数的整个主体包装在 @Lloyd 帮助的 ThreadPool.QueueUserWorkItem 中。

  public static void EmailShopDrawingAndDoorSchedule(IElevation elevation, string fromEmail, string toEmail)
    {
        ThreadPool.QueueUserWorkItem(t =>
                       {
                           using (Bitmap printCanvas = ShopDrawing.Merger.MergeElevationAndDoor(elevation, RotateFlipType.Rotate90FlipNone))
                           {
                               using (var ms = new MemoryStream())
                               {
                                   printCanvas.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                   ms.Position = 0;

                                   using (MailMessage mm = new MailMessage(new MailAddress(fromEmail), new MailAddress(toEmail)))
                                   {
                                       mm.Subject = "[Project: " + elevation.ProjectName + "] " + " Shop drawings for " + elevation.Name;
                                       mm.Body = "Your shop drawings are attached to this email in reference to Project: " + elevation.ProjectName + " -> Elevation: " + elevation.Name;

                                       using (Attachment at = new Attachment(ms, elevation.Name + ".png", "image/png"))
                                       {
                                           mm.Attachments.Add(at);

                                           using (var smtp = new SmtpClient())
                                           {
                                               smtp.Send(mm);
                                           };

                                       }
                                   };
                               };
                           };
                       });
    }

关于c# - SMTP.SendAsync 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15645688/

相关文章:

c# - 当 ReturnResult 是自定义对象时 Mock 返回 null 值,但当它是基本类型 bool 时按预期工作

c# - 空条件运算符

.net - .NET 4.0 迁移后引用 .NET 1.0 程序集的 System.TypeLoadException

javascript - 如何从异步调用返回响应?

c - 非规范(原始)模式下的异步串行通信并在 linux/osx 中生成 SIGIO

c# - WPF C#错误的鼠标坐标

C# 在 Windows 窗体或控制台上使用 HttpListener 和 Request.ServerVariables

c# - 代码契约(Contract) : false warning "Possibly unboxing a null reference"

c# - TPL数据流处理N条最新消息

asynchronous - spring security oauth2 与异步请求的使用