简体   繁体   中英

How can i send email one after one in a row?

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");

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

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.

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

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")

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

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

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 ?

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM