2

我正在尝试熟悉 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);

                };
            };
        };
    };
}
4

1 回答 1

2

将函数的整个主体包装在来自@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);
                                           };

                                       }
                                   };
                               };
                           };
                       });
    }
于 2013-03-26T20:58:58.353 回答