static void Main(string[] args) { string[] arRcpt = usercredentials.addEmailArray("[email protected] [email protected]"); int nRcpt = arRcpt.Length; SmtpMail[] arMail = new SmtpMail[nRcpt]; SmtpClient[] arSmtp = new SmtpClient[nRcpt]; SmtpClientAsyncResult[] arResult = new SmtpClientAsyncResult[nRcpt]; for (int i = 0; i < nRcpt; i++) { arMail[i] = new SmtpMail("TryIt"); arSmtp[i] = new SmtpClient(); } for (int i = 0; i < nRcpt; i++) { SmtpMail oMail = arMail[i]; // Set sender email address oMail.From = "*****@*****.**"; // Set recipient email address oMail.To = arRcpt[i]; // Set email subject oMail.Subject = "mass email test from c#"; // Set email body oMail.TextBody = "test from c#, this email is sent to " + arRcpt[i]; // Your smtp server address SmtpServer oServer = new SmtpServer("smtp.gmail.com"); oServer.Port = 587; // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "******"; oServer.Password = "******"; // If your smtp server requires SSL connection, please add this line oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; SmtpClient oSmtp = arSmtp[i]; // Submit email to BeginSendMail method and return // to process another email arResult[i] = oSmtp.BeginSendMail(oServer, oMail, null, null); Console.WriteLine(String.Format("Start to send email to {0} ...", arRcpt[i])); } // All emails were sent by BeginSendMail Method // now get result by EndSendMail method int nSent = 0; while (nSent < nRcpt) { for (int i = 0; i < nRcpt; i++) { // this email has been sent if (arResult[i] == null) { continue; } // wait for specified email ... if (!arResult[i].AsyncWaitHandle.WaitOne(10, false)) { continue; } try { // this email is finished, using EndSendMail to get result arSmtp[i].EndSendMail(arResult[i]); Console.WriteLine(String.Format("Send email to {0} successfully", arRcpt[i])); } catch (Exception ep) { Console.WriteLine( String.Format("Failed to send email to {0} with error {1}: ", arRcpt[i], ep.Message)); } // Set this email result to null, then it won't be processed again arResult[i] = null; nSent++; } } }
void _AddInstances( ref SmtpClient[] arSmtp, ref SmtpClientAsyncResult[] arResult, int index ) { int count = arSmtp.Length; for( int i = 0; i < count; i++ ) { SmtpClient oSmtp = arSmtp[i]; if( oSmtp == null ) { //idle instance found. oSmtp = new SmtpClient(); //store current list item index to object instance //and we can retrieve it in EASendMail events. oSmtp.Tag = index; //For evaluation usage, please use "TryIt" as the license code, otherwise the //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then //"trial version expired" exception will be thrown. //For licensed uasage, please use your license code instead of "TryIt", then the object //will never expire SmtpMail oMail = new SmtpMail("TryIt"); //If you want to specify a reply address //oMail.Headers.ReplaceHeader( "Reply-To: <reply@mydomain>" ); //From is a MailAddress object, in c#, it supports implicit converting from string. //The syntax is like this: "*****@*****.**" or "Tester<*****@*****.**>" //The example code without implicit converting // oMail.From = new MailAddress( "Tester", "*****@*****.**" ) // oMail.From = new MailAddress( "Tester<*****@*****.**>" ) // oMail.From = new MailAddress( "*****@*****.**" ) oMail.From = textFrom.Text; string name, address; ListViewItem item = lstTo.Items[index]; name = item.Text; address = item.SubItems[1].Text; oMail.To.Add( new MailAddress( name, address )); oMail.Subject = textSubject.Text; oMail.Charset = m_arCharset[lstCharset.SelectedIndex,1]; //replace keywords in body text. string body = textBody.Text; body = body.Replace( "[$subject]", oMail.Subject ); body = body.Replace( "[$from]", oMail.From.ToString()); body = body.Replace( "[$name]", name ); body = body.Replace( "[$address]", address ); oMail.TextBody = body; int y = m_arAttachment.Count; for( int x = 0; x < y; x++ ) { //add attachment oMail.AddAttachment( m_arAttachment[x] as string ); } SmtpServer oServer = new SmtpServer( textServer.Text ); oServer.Protocol = (ServerProtocol)lstProtocol.SelectedIndex; if( oServer.Server.Length != 0 ) { if( chkAuth.Checked ) { oServer.User = textUser.Text; oServer.Password = textPassword.Text; } if( chkSSL.Checked ) oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; } else { //To send email to the recipient directly(simulating the smtp server), //please add a Received header, //otherwise, many anti-spam filter will make it as junk email. System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-US"); string gmtdate = System.DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz", cur); gmtdate.Remove( gmtdate.Length - 3, 1 ); string recvheader = String.Format( "from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}", oServer.HeloDomain, gmtdate ); oMail.Headers.Insert( 0, new HeaderItem( "Received", recvheader )); } _CrossThreadSetItemText( "Connecting ...", index ); //Catching the following events is not necessary, //just make the application more user friendly. //If you use the object in asp.net/windows service or non-gui application, //You need not to catch the following events. //To learn more detail, please refer to the code in EASendMail EventHandler region oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler( OnIdle ); oSmtp.OnAuthorized += new SmtpClient.OnAuthorizedEventHandler( OnAuthorized ); oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler( OnConnected ); oSmtp.OnSecuring += new SmtpClient.OnSecuringEventHandler( OnSecuring ); oSmtp.OnSendingDataStream += new SmtpClient.OnSendingDataStreamEventHandler( OnSendingDataStream ); SmtpClientAsyncResult oResult = null; if( !chkTestRecipients.Checked ) { oResult = oSmtp.BeginSendMail( oServer, oMail, null, null ); } else { //Just test the email address without sending email data. oResult = oSmtp.BeginTestRecipients( null, oMail, null, null ); } //Add the object instance to the array. arSmtp[i] = oSmtp; arResult[i] = oResult; break; } } }