如何连续发送电子邮件?

时间:2013-08-12 17:43:36

标签: c# winforms email

在backgroundworkerdowork活动的Form1中,我做了:

se.SendPhotos(photofilesDir + "\\" + "photofiles.zip");
se.SendPhotos(photofilesDir1 + "\\" + "photofiles.zip");
se.SendPhotos(photofilesDir2 + "\\" + "photofiles.zip");
se.SendPhotos(photofilesDir3 + "\\" + "photofiles.zip");

在seE类SendEmail中我做了:

public void SendPhotos(string fileNameToSend)
        {
            try
            {
                MailAddress from = new MailAddress("test@gmail.com", "User " + (char)0xD8 + " Name",
                System.Text.Encoding.UTF8);
                MailAddress to = new MailAddress("test@test");
                photosmessage = new MailMessage(from, to);
                photosmessage.Body = "Please check the log file attachment I have some bugs.";
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
                photosmessage.Body += Environment.NewLine + someArrows;
                photosmessage.BodyEncoding = System.Text.Encoding.UTF8;
                photosmessage.Subject = "Log File For Checking Bugs" + someArrows;
                photosmessage.SubjectEncoding = System.Text.Encoding.UTF8;
                Attachment myAttachment = new Attachment(fileNameToSend, MediaTypeNames.Application.Octet);
                photosmessage.Attachments.Add(myAttachment);
                SmtpClient photossend = new SmtpClient("smtp.gmail.com", 587);
                photossend.SendCompleted += new SendCompletedEventHandler(photossend_SendCompleted);
                photossend.EnableSsl = true;
                photossend.Timeout = 10000;
                photossend.DeliveryMethod = SmtpDeliveryMethod.Network;
                photossend.UseDefaultCredentials = false;
                photossend.Credentials = new NetworkCredential("usern", "userpass");
                string userState = "test message1";
                photossend.SendAsync(photosmessage, userState);
                SendLogFile.Enabled = false;
                fname = fileNameToSend;
            }

            catch (Exception errors)
            {
                Logger.Write("Error sending message :" + errors);
            }
        }

        private void photossend_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            photosmessage.Dispose();
            if (fname == @"C:\Users\Simbalip\AppData\Local\outputphotos\photosfiles3" + "\\" + "photofiles.zip")
            {
                photossendended = true;
            }
        }

backgroundworkerdowork中的问题它永远不会发送最后一个photofilesDir3。 我使用了一个断点,它到达:photossendended = true; 但在我的电子邮件中,我只收到3个文件,而不是4个。

当我在背景工作者身上使用断点并且做了F11时,我看到它一个接一个地发送4个发送而没有等待。

每次photofiles.zip里面都有不同的文件。 现在我检查了一下断线:

if (fname == @"C:\Users\Simbalip\AppData\Local\outputphotos\photosfiles3" + "\\" + "photofiles.zip")

当它到达那里时它始终是真的它== fname从不C:\ Users \ Simbalip \ AppData \ Local \ outputphotos \ photosfiles2或1或photosfiles

但最终我得到3个不同的photofiles.zip文件,每个文件包含不同的文件,但其中一个文件从未发送过。

Myabe我需要以某种方式,当它发送电子邮件时,它会以某种方式直到第一个被发送然后发送下一个像在进程中有WaitForExit()所以也许这样的电子邮件?

1 个答案:

答案 0 :(得分:2)

SmtpClient的文档暗示它不支持并行操作。

  

如果正在进行电子邮件传输并且您再次调用SendAsync或Send,则会收到InvalidOperationException。

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

我建议在SendCompleted事件被提出之前不要发送下一封电子邮件。这意味着您的代码发生了巨大变化(每次调用SendPhotos时,实际上只会在您的流程在后台添加一些待处理的发送邮件操作)