我怎样才能一个接一个地发送电子邮件? [英] How can i send email one after one in a row?

查看:47
本文介绍了我怎样才能一个接一个地发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 backgroundworkerdowork 事件中的 Form1 中,我做了:

In Form1 in the backgroundworkerdowork event I did:

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

在我做过的 SendEmail 课程中:

In the se class SendEmail I did:

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 个.

The problem in the backgroundworkerdowork it never send the last one photofilesDir3. I used a breakpoint and its getting to : photossendended = true; But in my email im getting only 3 files sent for me and not 4.

当我在后台工作器上使用断点并执行 F11 时,我看到它在不等待的情况下一个接一个地执行 4 次发送.

When I used a breakpoint on the backgroundworker and did F11 I saw that its doing the 4 sendings one after one without waiting .

每次 photofiles.zip 里面都有不同的文件.现在我检查做了一个断点就行了:

Each time the photofiles.zip have different files inside. Now I check did a breakpoint on the line:

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

当它到达那里时,它一直都是真的 ==fname 从不 C:\Users\Simbalip\AppData\Local\outputphotos\photosfiles2 or 1 or photosfiles

And when it get there its all the time true it does == fname never C:\Users\Simbalip\AppData\Local\outputphotos\photosfiles2 or 1 or photosfiles

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

But in the end im getting 3 different photofiles.zip files each contain different files but one of the files never sent.

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

Myabe I need to make somehow that when it send email it will somehow untill the first one is sent then send the next one like in Process there is WaitForExit() so maybe something like that with the emails ?

推荐答案

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

The documentation for SmtpClient alludes to the fact that it doesn't support parallel operations.

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

If there is an e-mail transmission in progress and you call SendAsync or Send again, you will receive an InvalidOperationException.

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

我建议在 SendCompleted 事件引发之前不要发送下一封电子邮件.这意味着您的代码将发生巨大变化(每次调用 SendPhotos 实际上只是向您在后台处理的待处理发送邮件操作集合中添加了一些内容)

I would recommend not sending the next email until the SendCompleted event has been raised. This means a drastic change in your code (where each call to SendPhotos really just adds something to a collection of pending send mail operations that your process in the background)

这篇关于我怎样才能一个接一个地发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆