如何使这将发送电子邮件只有一次? [英] How do i make that it will send the email only once?

查看:94
本文介绍了如何使这将发送电子邮件只有一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 如果真的保持检查。
于是,我就做,而(真)

But i want it to do it once only and the keep check for the se.photossendended if its true. So i tried to do 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;
          }
     }
}



但随后将持有的所有该程序,绝不会让它正确的,因为该程序没有继续和所有的卡在这个循环。
因此,它从来没有真正的和循环将永远保留。

But then it will hold all the program and will never make it true since the program is not continue and its all stuck in this loop. So its never true and the loop will keep forever.

编辑**

这是在SE类SendEmail

This is the se class 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;
然后在Form1中的定时器4 Tick事件我想发送的电子邮件一次如果真的停止计时器激活计时器5然后发送有第二个电子邮件,一遍又一遍。

I wanted to make sure the email sent so i did that it will be true: photossendended = true; Then in Form1 in timer4 tick event i want to send the email once if its true stop the timer activate timer 5 then send there the second email and over and over again.

我有4个计时器滴答事件我diasble和一个使他们之一。
的原因是,我想给只有当人之前已经完成发送每封电子邮件。

I have 4 timers tick events i diasble and enable them one by one. The reason is that i wanted to send each email only when the one before had finished to be sent.

推荐答案

我猜你要发送邮件异步不会阻塞UI,而且还想等到发送带有下一个邮件,然后再继续完成。

I guess you are trying to send mails async without blocking the UI, but want also to wait till sending is completed before continuing with next mail.

如果您使用的是C# 5 / .NET 4.5,您可以在 SendMailAsync =htt​​p://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx 相对=nofollow> 异步 方法:

If you are using c#5/.Net 4.5 You can use SendMailAsync in an async method as:

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



那么,你的方法可以是这样的。

So, your method can be something like this

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

这篇关于如何使这将发送电子邮件只有一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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