c# - How can i send email one after one in a row? -


in form1 in backgroundworkerdowork event did:

se.sendphotos(photofilesdir + "\\" + "photofiles.zip"); se.sendphotos(photofilesdir1 + "\\" + "photofiles.zip"); se.sendphotos(photofilesdir2 + "\\" + "photofiles.zip"); se.sendphotos(photofilesdir3 + "\\" + "photofiles.zip"); 

in se class sendemail did:

public void sendphotos(string filenametosend)         {             try             {                 mailaddress = new mailaddress("test@gmail.com", "user " + (char)0xd8 + " name",                 system.text.encoding.utf8);                 mailaddress = new mailaddress("test@test");                 photosmessage = new mailmessage(from, to);                 photosmessage.body = "please check log file attachment have 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 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 backgroundworkerdowork never send last 1 photofilesdir3. used breakpoint , getting : photossendended = true; in email im getting 3 files sent me , not 4.

when used breakpoint on backgroundworker , did f11 saw doing 4 sendings 1 after 1 without waiting .

each time photofiles.zip have different files inside. check did breakpoint on line:

if (fname == @"c:\users\simbalip\appdata\local\outputphotos\photosfiles3" + "\\" + "photofiles.zip") 

and when there time true == fname never c:\users\simbalip\appdata\local\outputphotos\photosfiles2 or 1 or photosfiles

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

myabe need make somehow when send email somehow untill first 1 sent send next 1 in process there waitforexit() maybe emails ?

the documentation smtpclient alludes fact doesn't support parallel operations.

if there e-mail transmission in progress , call sendasync or send again, receive invalidoperationexception.

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

i recommend not sending next email until sendcompleted event has been raised. means drastic change in code (where each call sendphotos adds collection of pending send mail operations process in background)


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -