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

时间:2013-08-12 18:28:17

标签: c# winforms

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个计时器,我可以将这些事件分开并逐个启用它们。 原因是我只想在发送之前发送每封电子邮件。

3 个答案:

答案 0 :(得分: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<object>(null);
    }
}

你可以用它

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

PS:现在,您方法的更好名称是SendPhotosAsync

答案 1 :(得分:2)

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;   
    }            
}

我认为您应该公开类SendCompleted的事件SendEmail,以便我们可以执行以下操作:

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

答案 2 :(得分:0)

我不知道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;
          }
     }
}

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