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 に答える