我怎么做它只发送一次电子邮件?

private void timer4_Tick(object sender, EventArgs e) { se.SendPhotos(photofilesDir + "\\" + "photofiles.zip"); if (se.photossendended == true) { se.photossendended = false; timer4.Enabled = false; timer5.Enabled = true; } } 

直到se.photossendended == true它将继续制作se.SendPhotos(photofilesDir + "\\" + "photofiles.zip");

但我希望它只做一次,并保持检查se.photossendended如果它是真的。 所以我试着做while(true)

 private void timer4_Tick(object sender, EventArgs e) { se.SendPhotos(photofilesDir + "\\" + "photofiles.zip"); while(true) { if (se.photossendended == true) { se.photossendended = false; timer4.Enabled = false; timer5.Enabled = true; } } } 

但是它将保留所有程序,并且永远不会成功,因为程序不会继续并且它全部停留在这个循环中。 所以它永远不会真实,循环将永远存在。

编辑**

这是se类SendEmail

 public void SendPhotos(string fileNameToSend) { try { MailAddress from = new MailAddress("username", "User " + (char)0xD8 + " Name", System.Text.Encoding.UTF8); MailAddress to = new MailAddress("myrmail"); 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("user", "pass"); string userState = "test message1"; photossend.SendAsync(photosmessage, userState); SendLogFile.Enabled = false; } catch (Exception errors) { Logger.Write("Error sending message :" + errors); } } private void photossend_SendCompleted(object sender, AsyncCompletedEventArgs e) { photosmessage.Dispose(); photossendended = true; } 

我想确保发送的电子邮件,所以我这样做是真的:photossendended = true; 然后在timer4 tick4事件的Form1中我想发送一次电子邮件,如果它真正停止计时器激活计时器5然后再发送第二封电子邮件。

我有4个计时器,我可以将这些事件分开,并逐个启用它们。 原因是我只想在发送之前发送每封电子邮件。

我猜你试图在不阻止用户界面的情况下发送邮件异步,但还要等到发送完成后继续下一封邮件。

如果您使用的是c#5 / .Net 4.5您可以在async方法中使用SendMailAsync

 async void SendMails() { await server.SendMailAsync(mailMessage1); await server.SendMailAsync(mailMessage2); } 

所以,你的方法可以是这样的

 public Task SendPhotos(string fileNameToSend) { try { MailAddress from = new MailAddress("username", "User " + (char)0xD8 + " Name", System.Text.Encoding.UTF8); MailAddress to = new MailAddress("myrmail"); var 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.EnableSsl = true; photossend.Timeout = 10000; photossend.DeliveryMethod = SmtpDeliveryMethod.Network; photossend.UseDefaultCredentials = false; photossend.Credentials = new NetworkCredential("user", "pass"); SendLogFile.Enabled = false; return photossend.SendMailAsync(photosmessage); } catch (Exception errors) { Logger.Write("Error sending message :" + errors); return Task.FromResult(null); } } 

你可以使用它

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

PS:现在你方法的一个更好的名字是SendPhotosAsync

 private void timer4_Tick(object sender, EventArgs e) { if(!se.photossendended) { se.SendPhotos(photofilesDir + "\\" + "photofiles.zip"); se.photossendended = true; timer4.Enabled = false; timer5.Enabled = true; } } 

看起来您想要异步发送电子邮件。 你几乎完成了photossend_SendCompleted的代码。 其余代码应如下:

 bool sendingStarted; private void timer4_Tick(object sender, EventArgs e) { if(!sendingStarted) { se.SendPhotos(photofilesDir + "\\" + "photofiles.zip"); sendingStarted = true; } if(photossended){ timer4.Enabled = false; timer5.Enabled = true; } } 

我认为你应该暴露类SendEmail的事件SendCompleted ,这样我们就可以这样做:

 se.SendCompleted += (s,e) => { timer4.Enabled = false; timer5.Enabled = true;//Of course we still need the flag sendingStarted. }; 

我不知道se.SendPhotos看起来像什么,但你可以这样做

 private void timer4_Tick(object sender, EventArgs e) { se.SendPhotos(photofilesDir + "\\" + "photofiles.zip"); while(true) { if (se.photossendended == true) { se.photossendended = false; timer4.Enabled = false; timer5.Enabled = true; break; } } } 

但这更像是一个黑客而不是一个解决方案:/