private void button1_Click(object sender, System.EventArgs e) { if( textFrom.Text.Length == 0 ) { MessageBox.Show( "Please input From!, the format can be [email protected] or Tester<*****@*****.**>" ); return; } if( textTo.Text.Length == 0 && textCc.Text.Length == 0 ) { MessageBox.Show( "Please input To or Cc!, the format can be [email protected] or Tester<*****@*****.**>, please use , or ; to separate multiple recipients" ); return; } btnSend.Enabled = false; btnCancel.Enabled = true; m_bcancel = false; //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"); SmtpClient oSmtp = new SmtpClient(); //To generate a log file for SMTP transaction, please use //oSmtp.LogFileName = "c:\\smtp.log"; string err = ""; try { oMail.Reset(); //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; //To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string. // multiple address are separated with (,;) //The syntax is like this: "[email protected], [email protected]" //The example code without implicit converting // oMail.To = new AddressCollection( "[email protected], [email protected]" ); // oMail.To = new AddressCollection( "Tester1<*****@*****.**>, Tester2<*****@*****.**>"); oMail.To = textTo.Text; //You can add more recipient by Add method // oMail.To.Add( new MailAddress( "tester", "*****@*****.**")); oMail.Cc = textCc.Text; oMail.Subject = textSubject.Text; oMail.Charset = m_arCharset[lstCharset.SelectedIndex, 1]; //Digital signature and encryption if(!_SignEncrypt( ref oMail )) { btnSend.Enabled = true; btnCancel.Enabled = false; return; } string body = textBody.Text; body = body.Replace( "[$from]", oMail.From.ToString()); body = body.Replace( "[$to]", oMail.To.ToString()); body = body.Replace( "[$subject]", oMail.Subject ); if( chkHtml.Checked ) oMail.HtmlBody = body; else oMail.TextBody = body; int count = m_arAttachment.Count; for( int i = 0; i < count; i++ ) { //Add attachment oMail.AddAttachment( m_arAttachment[i] 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 )); } //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 ); if( oServer.Server.Length == 0 && oMail.Recipients.Count > 1 ) { //To send email without specified smtp server, we have to send the emails one by one // to multiple recipients. That is because every recipient has different smtp server. _DirectSend( ref oMail, ref oSmtp ); } else { sbStatus.Text = "Connecting ... "; pgSending.Value = 0; oSmtp.SendMail( oServer, oMail ); MessageBox.Show( String.Format( "The message was sent to {0} successfully!", oSmtp.CurrentSmtpServer.Server )); sbStatus.Text = "Completed"; } //If you want to reuse the mail object, please reset the Date and Message-ID, otherwise //the Date and Message-ID will not change. //oMail.Date = System.DateTime.Now; //oMail.ResetMessageID(); //oMail.To = "*****@*****.**"; //oSmtp.SendMail( oServer, oMail ); } catch( SmtpTerminatedException exp ) { err = exp.Message; } catch( SmtpServerException exp ) { err = String.Format( "Exception: Server Respond: {0}", exp.ErrorMessage ); } catch( System.Net.Sockets.SocketException exp ) { err = String.Format( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message ); } catch( System.ComponentModel.Win32Exception exp ) { err = String.Format( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message ); } catch( System.Exception exp ) { err = String.Format( "Exception: Common: {0}", exp.Message ); } if( err.Length > 0 ) { MessageBox.Show( err ); sbStatus.Text = err; } //to get more debug information, please use //MessageBox.Show( oSmtp.SmtpConversation ); btnSend.Enabled = true; btnCancel.Enabled = false; }
private void btnSimple_Click(object sender, System.EventArgs e) { if( textFrom.Text.Length == 0 ) { MessageBox.Show( "Please input From, the format can be [email protected] or Tester<*****@*****.**>" ); return; } int to_count = lstTo.Items.Count; if( to_count == 0 ) { MessageBox.Show( "please add a recipient at least!" ); return; } MessageBox.Show( "Simple Send will send email with single thread, the code is vey simple.\r\nIf you don't want the extreme performance, the code is recommended to beginer!" ); btnSend.Enabled = false; btnSimple.Enabled = false; btnAdd.Enabled = false; btnClear.Enabled = false; btnAddTo.Enabled = false; btnClearTo.Enabled = false; chkTestRecipients.Enabled =false; btnCancel.Enabled = true; m_bcancel = false; int sent = 0; for( sent = 0; sent < to_count; sent++ ) { lstTo.Items[sent].SubItems[2].Text = "Ready"; } m_ntotal = to_count; m_nsent = 0; m_nsuccess = 0; m_nfailure = 0; status.Text = String.Format( "Total {0}, Finished {1}, Succeeded {2}, Failed {3}", m_ntotal, m_nsent, m_nsuccess, m_nfailure ); sent = 0; while( sent < to_count && !m_bcancel) { Application.DoEvents(); int index = sent; sent++; //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"); SmtpClient oSmtp = new SmtpClient(); oSmtp.Tag = index; //To generate a log file for SMTP transaction, please use //oSmtp.LogFileName = "c:\\smtp.log"; string err = ""; try { oMail.Reset(); //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; //To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string. // multiple address are separated with (,;) //The syntax is like this: "[email protected], [email protected]" //The example code without implicit converting // oMail.To = new AddressCollection( "[email protected], [email protected]" ); // oMail.To = new AddressCollection( "Tester1<*****@*****.**>, Tester2<*****@*****.**>"); 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 )); } //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 ); _CrossThreadSetItemText( "Connecting...", index ); if(!chkTestRecipients.Checked) { oSmtp.SendMail( oServer, oMail ); _CrossThreadSetItemText( "Completed", index ); } else { oSmtp.TestRecipients( null, oMail ); _CrossThreadSetItemText( "PASS", index ); } m_nsuccess ++; //If you want to reuse the mail object, please reset the Date and Message-ID, otherwise //the Date and Message-ID will not change. //oMail.Date = System.DateTime.Now; //oMail.ResetMessageID(); //oMail.To = "*****@*****.**"; //oSmtp.SendMail( oServer, oMail ); } catch( SmtpTerminatedException exp ) { err = exp.Message; _CrossThreadSetItemText( err, index ); m_nfailure++; } catch( SmtpServerException exp ) { err = String.Format( "Exception: Server Respond: {0}", exp.ErrorMessage ); _CrossThreadSetItemText( err, index ); m_nfailure++; } catch( System.Net.Sockets.SocketException exp ) { err = String.Format( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message ); _CrossThreadSetItemText( err, index ); m_nfailure++; } catch( System.ComponentModel.Win32Exception exp ) { err = String.Format( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message ); _CrossThreadSetItemText( err, index ); m_nfailure++; } catch( System.Exception exp ) { err = String.Format( "Exception: Common: {0}", exp.Message ); _CrossThreadSetItemText( err, index ); m_nfailure++; } m_nsent++; status.Text = String.Format( "Total {0}, Finished {1}, Succeeded {2}, Failed {3}", m_ntotal, m_nsent, m_nsuccess, m_nfailure); } if( m_bcancel ) { for( ; sent < to_count; sent++ ) { lstTo.Items[sent].SubItems[2].Text = "Operation was cancelled"; } } btnSend.Enabled = true; btnSimple.Enabled = true; btnAdd.Enabled = true; btnClear.Enabled = true; btnAddTo.Enabled = true; btnClearTo.Enabled = true; chkTestRecipients.Enabled = true; btnCancel.Enabled = false; }
private void btnSend_Click(object sender, System.EventArgs e) { if (textFrom.Text.Length == 0) { MessageBox.Show("Please input From!, the format can be [email protected] or Tester<*****@*****.**>"); return; } if (textTo.Text.Length == 0 && textCc.Text.Length == 0) { MessageBox.Show("Please input To or Cc!, the format can be [email protected] or Tester<*****@*****.**>, please use , or ; to separate multiple recipients"); return; } if (textServer.Text.Length == 0) { MessageBox.Show("Please input server address"); return; } btnSend.Enabled = false; btnCancel.Enabled = true; m_bcancel = false; //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"); SmtpClient oSmtp = new SmtpClient(); //To generate a log file for SMTP transaction, please use //oSmtp.LogFileName = "c:\\smtp.log"; string err = ""; try { oMail.Reset(); //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; //To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string. // multiple address are separated with (,;) //The syntax is like this: "[email protected], [email protected]" //The example code without implicit converting // oMail.To = new AddressCollection( "[email protected], [email protected]" ); // oMail.To = new AddressCollection( "Tester1<*****@*****.**>, Tester2<*****@*****.**>"); oMail.To = textTo.Text; //You can add more recipient by Add method // oMail.To.Add( new MailAddress( "tester", "*****@*****.**")); oMail.Cc = textCc.Text; oMail.Subject = textSubject.Text; string body = textBody.Text; oMail.TextBody = textBody.Text; int count = m_arAttachment.Count; for (int i = 0; i < count; i++) { // //Add attachment oMail.AddAttachment(m_arAttachment[i] as string); } SmtpServer oServer = new SmtpServer(textServer.Text); if (chkAuth.Checked) { oServer.User = textUser.Text; oServer.Password = textPassword.Text; } if (chkSSL.Checked) { // Use SSL/TLS based on server port oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; } else { // Most mordern SMTP servers require SSL/TLS connection now // ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically oServer.ConnectType = SmtpConnectType.ConnectTryTLS; } //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); sbStatus.Text = "Connecting ... "; pgSending.Value = 0; oSmtp.SendMail(oServer, oMail); MessageBox.Show(String.Format("The message was sent to {0} successfully!", oSmtp.CurrentSmtpServer.Server)); sbStatus.Text = "Completed"; //If you want to reuse the mail object, please reset the Date and Message-ID, otherwise //the Date and Message-ID will not change. //oMail.Date = System.DateTime.Now; //oMail.ResetMessageID(); //oMail.To = "*****@*****.**"; //oSmtp.SendMail( oServer, oMail ); } catch (SmtpTerminatedException exp) { err = exp.Message; } catch (SmtpServerException exp) { err = String.Format("Exception: Server Respond: {0}", exp.ErrorMessage); } catch (System.Net.Sockets.SocketException exp) { err = String.Format("Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message); } catch (System.ComponentModel.Win32Exception exp) { err = String.Format("Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message); } catch (System.Exception exp) { err = String.Format("Exception: Common: {0}", exp.Message); } if (err.Length > 0) { MessageBox.Show(err); sbStatus.Text = err; } //to get more debug information, please use //MessageBox.Show( oSmtp.SmtpConversation ); btnSend.Enabled = true; btnCancel.Enabled = false; }
private void button1_Click(object sender, System.EventArgs e) { if (textFrom.Text.Length == 0) { MessageBox.Show("Please input From!, the format can be [email protected] or Tester<*****@*****.**>"); return; } if (textTo.Text.Length == 0 && textCc.Text.Length == 0) { MessageBox.Show("Please input To or Cc!, the format can be [email protected] or Tester<*****@*****.**>, please use , or ; to separate multiple recipients"); return; } btnSend.Enabled = false; btnCancel.Enabled = true; m_bcancel = false; //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"); SmtpClient oSmtp = new SmtpClient(); //To generate a log file for SMTP transaction, please use //oSmtp.LogFileName = "c:\\smtp.log"; string err = ""; try { oMail.Reset(); //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; //To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string. // multiple address are separated with (,;) //The syntax is like this: "[email protected], [email protected]" //The example code without implicit converting // oMail.To = new AddressCollection( "[email protected], [email protected]" ); // oMail.To = new AddressCollection( "Tester1<*****@*****.**>, Tester2<*****@*****.**>"); oMail.To = textTo.Text; //You can add more recipient by Add method // oMail.To.Add( new MailAddress( "tester", "*****@*****.**")); oMail.Cc = textCc.Text; oMail.Subject = textSubject.Text; oMail.Charset = m_arCharset[lstCharset.SelectedIndex, 1]; //Digital signature and encryption if (!_SignEncrypt(ref oMail)) { btnSend.Enabled = true; btnCancel.Enabled = false; return; } string body = textBody.Text; body = body.Replace("[$from]", oMail.From.ToString()); body = body.Replace("[$to]", oMail.To.ToString()); body = body.Replace("[$subject]", oMail.Subject); if (chkHtml.Checked) { oMail.HtmlBody = body; } else { oMail.TextBody = body; } int count = m_arAttachment.Count; for (int i = 0; i < count; i++) { //Add attachment oMail.AddAttachment(m_arAttachment[i] 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)); } //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); if (oServer.Server.Length == 0 && oMail.Recipients.Count > 1) { //To send email without specified smtp server, we have to send the emails one by one // to multiple recipients. That is because every recipient has different smtp server. _DirectSend(ref oMail, ref oSmtp); } else { sbStatus.Text = "Connecting ... "; pgSending.Value = 0; oSmtp.SendMail(oServer, oMail); MessageBox.Show(String.Format("The message was sent to {0} successfully!", oSmtp.CurrentSmtpServer.Server)); sbStatus.Text = "Completed"; } //If you want to reuse the mail object, please reset the Date and Message-ID, otherwise //the Date and Message-ID will not change. //oMail.Date = System.DateTime.Now; //oMail.ResetMessageID(); //oMail.To = "*****@*****.**"; //oSmtp.SendMail( oServer, oMail ); } catch (SmtpTerminatedException exp) { err = exp.Message; } catch (SmtpServerException exp) { err = String.Format("Exception: Server Respond: {0}", exp.ErrorMessage); } catch (System.Net.Sockets.SocketException exp) { err = String.Format("Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message); } catch (System.ComponentModel.Win32Exception exp) { err = String.Format("Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message); } catch (System.Exception exp) { err = String.Format("Exception: Common: {0}", exp.Message); } if (err.Length > 0) { MessageBox.Show(err); sbStatus.Text = err; } //to get more debug information, please use //MessageBox.Show( oSmtp.SmtpConversation ); btnSend.Enabled = true; btnCancel.Enabled = false; }