Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sending Email in C Isharuye

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

http://www.emailarchitect.net/easendmail/kb/csharp.aspx?

cat=14

EASendMail SMTP Component > Developer Center > Send Email over SSL in C#

Send Email over SSL in C#


In previous section, I introduced the basic things of email sending in C#. In this section, I will introduce the SSL connection. SSL connection encrypts data between the SMTP component and SMTP server to protects user, password and email content in TCP/IP level. Now this technology is commonly used and many SMTP servers are deployed with SSL such as gmail, yahoo and hotmail. There are two ways to deploy SSL on SMTP server: 1. Using STARTTLS command to switch SSL channel on normal SMTP port (25 or 587); 2. Deploying SSL on another port (465 or other port, you may query it from your server administrator) directly. EASendMail SMTP component supports both ways. The connection can be specified by EASendMail.SmtpConnectType enumeration. Please see the following example code.

[C# Exmaple - SSL/TLS]


// Send email by normal TCP/IP without SSL connection SmtpServer oServer = new SmtpServer("localhost 25"); oServer.ConnectType = SmtpConnectType.ConnectNormal; // Send email by SSL connection with STARTTLS command switching SmtpServer oServer = new SmtpServer("localhost 25"); oServer.ConnectType = SmtpConnectType.ConnectSTARTTLS; // Send email by SSL connection with direct SSL. SmtpServer oServer = new SmtpServer("localhost 465"); oServer.ConnectType = SmtpConnectType.ConnectDirectSSL; // Send email by SSL connection with auto-detect. // If port is 25 or 587, STARTTLS SSL will be used; otherwise direct SSL will be used. SmtpServer oServer = new SmtpServer("localhost 465"); oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; SmtpServer oServer = new SmtpServer("localhost 25"); oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email over direct SSL on 465 port]


The following example codes demonstrate how to use EASendMail SMTP component to send email with direct SSL connection on 465 port. To get the full samples of EASendMail, please refer to Samples section.

using using using using

System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); oMail.From = "test@emailarchitect.net"; oMail.To = "support@emailarchitect.net"; oMail.Subject = "test email from c# project"; oMail.TextBody = "this is a test email sent from c# project, do not reply"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // Set SSL 465 port oServer.Port = 465; // Set direct SSL connection oServer.ConnectType = SmtpConnectType.ConnectDirectSSL; // User and password for ESMTP authentication oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; try { Console.WriteLine("start to send email ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

[C# Example - Send email over TLS on 25 or 587 port]


The following example codes demonstrate how to use EASendMail SMTP component to send email with TLS (STARTTLS command) connection on 25 port. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); oMail.From = "test@emailarchitect.net"; oMail.To = "support@emailarchitect.net"; oMail.Subject = "test email from c# project"; oMail.TextBody = "this is a test email sent from c# project, do not reply"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // Set 25 port oServer.Port = 25; // Set TLS connection oServer.ConnectType = SmtpConnectType.ConnectSTARTTLS; // User and password for ESMTP authentication oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; try { Console.WriteLine("start to send email ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

Next Section At next section I will introduce how to send email using Gmail account. <- Prev: A simple C# project | Next : Send email using Gmail in C# -> Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email using Gmail in C#

Send Email using Gmail in C#


In previous section, I introduced how to send email over SSL connection. In this section, I will introduce how to use your Gmail account to send email in C#. Gmail SMTP server address is "smtp.gmail.com". It requires SSL or TLS connection, and you should use your Gmail email address as the user name for ESMTP authentication. For example: your email is "gmailid@gmail.com", and then the user name should be "gmailid@gmail.com". Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email using Gmail account over TLS/SSL connection]
The following example codes demonstrate how to use EASendMail SMTP component to send email using Gmail account. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Your gmail email address oMail.From = "gmailid@gmail.com"; // Set recipient email address oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email from gmail account"; // Set email body oMail.TextBody = "this is a test email sent from c# project with gmail."; // Gmail SMTP server address SmtpServer oServer = new SmtpServer("smtp.gmail.com");

// If you want to use direct SSL 465 port, // please add this line, otherwise TLS will be used. // oServer.Port = 465; // detect SSL/TLS automatically oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; // Gmail user authentication // For example: your email is "gmailid@gmail.com", then the user should be the same oServer.User = "gmailid@gmail.com"; oServer.Password = "yourpassword"; try { Console.WriteLine("start to send email over SSL ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

Next Section At next section I will introduce how to send email with Yahoo account. <- Prev: Send email over SSL in C# | Next: Send email using Yahoo in C# - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email using Yahoo in C#

Send Email using Yahoo in C#


In previous section, I introduced how to send email using Gmail account. In this section, I will introduce how to use your Yahoo account to send email in C#. Yahoo SMTP server address is "smtp.mail.yahoo.com". It supports both Normal/SSL connection to do user authentication, and you should use your Yahoo email address as the

user name for ESMTP authentication. For example: your email is "myid@yahoo.com", and then the user name should be "myid@yahoo.com". If you want to use SSL connection with Yahoo SMTP server, you must set the port to 465. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email using Yahoo account over direct SSL connection]
The following example codes demonstrate how to use EASendMail SMTP component to send email using Yahoo account. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Your yahoo email address oMail.From = "myid@yahoo.com"; // Set recipient email address oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email from yahoo account"; // Set email body oMail.TextBody = "this is a test email sent from c# project with yahoo."; // Yahoo SMTP server address SmtpServer oServer = new SmtpServer("smtp.mail.yahoo.com"); // For example: your email is "myid@yahoo.com", then the user should be "myid@yahoo.com" oServer.User = "myid@yahoo.com"; oServer.Password = "yourpassword"; // Because yahoo deploys SMTP server on 465 port with direct SSL connection. // So we should change the port to 465. oServer.Port = 465; // detect SSL type automatically

oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send email over SSL ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

Next Section At next section I will introduce how to send email using Hotmail/MSN Live account. <- Prev: Send email using Gmail in C# -> | Next: Send email using Hotmail/MSN Live in C# -> Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email using Hotmail/MSN Live in C#

Send Email using Hotmail/MSN Live in C#


In previous section, I introduced how to send email using Yahoo account. In this section, I will introduce how to use your Hotmail/MSN Live account to send email in C#. Hotmail/MSN Live SMTP server address is "smtp.live.com". It requires TLS connection to do user authentication, and you should use your Hotmail/MSN Live email address as the user name for ESMTP authentication. For example: your email is "liveid@hotmail.com", and then the user name should be "myid@hotmail.com". Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email using Hotmail/MSN Live account over TLS connection]
The following example codes demonstrate how to use EASendMail SMTP component to send email using Hotmail/MSN Live account. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Your Hotmail email address oMail.From = "liveid@hotmail.com"; // Set recipient email address oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email from hotmail account"; // Set email body oMail.TextBody = "this is a test email sent from c# project with hotmail."; // Hotmail SMTP server address SmtpServer oServer = new SmtpServer("smtp.live.com"); // Hotmail user authentication should use your // email address as the user name. oServer.User = "liveid@hotmail.com"; oServer.Password = "yourpassword"; // detect SSL/TLS connection automatically oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send email over SSL..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } }

Next Section At next section I will introduce how to send email without specified SMTP server. <- Prev: Send email using Yahoo in C# | Next: Send email directly without SMTP server(MX DNS lookup) - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email directly without SMTP server(MX DNS lookup) in C#

Send Email directly without SMTP server(MX DNS lookup) in C#


In previous section, I introduced how to send email using Hotmail/MSN Live account. In this section, I will introduce how to use DNS lookup to send email without specified SMTP server in C#. In general, we send email via specified SMTP server. How does the specified SMTP server know what address this email should be sent to? The answer is... it queries MX record of recipient's domain via DNS lookup. It then forwards this email to the SMTP server queried from DNS server. If recipient's server doesn't work fine, sender's SMTP server will send a failure-delivery report to the sender telling it failed to send out the email. How does EASendMail SMTP component work with "Send email directly"? Firstly, it queries MX record for recipient address from DNS, then sends email to recipient's email server directly. In short, if no SMTP server is specified in the code, EASendMail will send email to recipient directly. Since querying DNS server consumes CPU time and networking resource, the performance of "Send email directly" is lower than sending email with specified SMTP server. Moreover, nowadays more and more SMTP servers block email sent from dynamic IP address, so we don't recommend you to use "Direct Send Email" except you have a static IP address or you encounter problem with your ISP SMTP server. Every recipient may have different SMTP server, if there are multiple recipients in one message and you want to send email directly, you should send the email to the recipients one by one. To implement this feature, you just need to put nothing to SMTP server address.

Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email without specified SMTP server (MX record DNS lookup)]
The following example codes demonstrate how to use EASendMail SMTP component to send email using DNS lookup. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "direct email sent from c# project"; // Set email body oMail.TextBody = "this is a test email sent from c# project directly"; // Set SMTP server address to "". SmtpServer oServer = new SmtpServer(""); // Do not set user authentication // Do not set SSL connection try { Console.WriteLine("start to send email directly ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } }

With above code, if you get error like "5xx IP address rejected", that means your IP address is blocked by the recipient's SMTP server. You have to specify a SMTP server with user authentication to relay your email. Next Section At next section I will introduce how to compose HTML email. <- Prev: Send email using Hotmail/MSN Live | Next: Send HTML email in C# - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send HTML Email in C#

Send HTML Email in C#


In previous section, I introduced how to send email without specified SMTP server. In this section, I will introduce how to compose and send HTML email in C#. If you want to specify the font, color or insert pictures in your email, you should use Html email format instead of Plain text email format. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send HTML email]


The following example codes demonstrate how to use EASendMail SMTP component to send email in HTML body format. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args)

{ SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test html email from C#"; // Set Html body oMail.HtmlBody = "<font size=5>This is</font> <font color=red><b>a test</b></font>"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send HTML email ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

After you received the email by your email client, the body text is like this:

Of course, you don't have to write the HTML source body text in your application manually. You can build a html file with HTML tools and use ImportHtmlBody method to import the html file directly. You can also refer to the htmlmail.* samples in EASendMail Installer. Those samples demonstrate how to build a HTML email editor and send HTML email with attachment or embedded images/pictures.

Next Section At next section I will introduce how to attach file attachment to email message. <- Prev: Send email directly without SMTP server(MX DNS lookup) | Next: Send email with attachment - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email with Attachment in C#

Send Email with Attachment in C#


In previous section, I introduced how to send HTML email. In this section, I will introduce how to add attachment to email in C#. To send an email with file attachment, we need to use AddAttachment method. This method can attach a file to the email message from local disk or a remote URL. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Add attachment from local disk or remote URL]


The following example codes demonstrate how to use EASendMail SMTP component to send email with file attachments. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test html email with attachment"; // Set Html body oMail.HtmlBody = "<font size=\"5\">This is</font> <font color=\"red\"><b>a test</b></font>"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

// User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { // Add attachment from local disk oMail.AddAttachment( "d:\\test.pdf" ); // Add attachment from remote website oMail.AddAttachment( "http://www.emailarchitect.net/webapp/img/logo.jpg" ); Console.WriteLine("start to send email with attachment ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

Next Section At next section I will introduce how to add embedded images/pictures to email message. <- Prev: Send HTML email | Next: Send email with embedded images - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email with Embedded Images in C#

Send Email with Embedded Images in C#

In previous section, I introduced how to send email with file attachment. In this section, I will introduce how to add embedded images to email in C#. To attach an embedded images to email, you should add an attachment to email at first. Then you should assign an unique identifier(contentid) to this attachment. Finally, you need to replace the <img src="your file name" /> to <img src="cid:yourcontentid" />. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Add embedded images to email]


The following example codes demonstrate how to use EASendMail SMTP component to send email with embedded images. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test html email with attachment"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; // If your SMTP server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { // Add image attachment from local disk

Attachment oAttachment = oMail.AddAttachment( "d:\\test.gif" ); // Specifies the attachment as an embedded image // contentid can be any string. string contentID = "test001@host"; oAttachment.ContentID = contentID; oMail.HtmlBody = "<html><body>this is a <img src=\"cid:" + contentID + "\"> embedded image.</body></html>"; Console.WriteLine("start to send email with embedded image..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

To attach embedded images/pictures, SmtpMail.ImportHtmlBody and SmtpMail.ImportHtml methods are strongly recommended. With these methods, you don't have to specify the ContentID manually. The html source/file html body can be imported to email with embedded pictures automatically. You can also refer to the htmlmail.* samples in EASendMail Installer. Those samples demonstrate how to build a HTML email editor and send HTML email with attachment or embedded images/pictures.

Next Section

At next section I will introduce how to sign email with digital certificate. <- Prev: Send email with attachment | Next: Send email with digital signature (S/MIME) - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email with Digital Signature in C# - S/MIME

Send Email with Digital Signature in C# S/MIME


In previous section, I introduced how to send email with embedded images. In this section, I will introduce how to sign email with digital certificate in C#. Digital signature prevents email content is faked or changed in transport level. Encrypting email protects email content from exposure to inappropriate recipients. Both digital signature and email encrypting depend on digital certificate. If you have an email digital signature certificate installed on your machine, you can find it in "Control Panel" -> "Internet Options" -> "Content" -> "Certificates" -> "Personal".

Then you can use your email certificate to sign the email by the following code. If you don't have a certificate for your email address, you MUST get a digital certificate for personal email protection from third-party certificate authorities such as www.verisign.com. If you need a free certificate for your email address, you can go to http://www.comodo.com/home/email-security/free-email-certificate.php to apply for one year free email certificate. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email with digital signature (S/MIME)]


The following example codes demonstrate how to use EASendMail SMTP component to sign email with digital certificate. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program {

static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email with digital signature"; // Set email body oMail.TextBody = "this is a test email with digital signature"; // Your smtp server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; try { // Find certificate by email adddress in My Personal Store. // Once the certificate is loaded to From, the email content // will be signed automatically oMail.From.Certificate.FindSubject(oMail.From.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "My"); } catch (Exception exp) { Console.WriteLine("No sign certificate found for <" + oMail.From.Address + ">:" + exp.Message); } // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send email with digital signature ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } }

} }

Next Section At next section I will introduce how to encrypt email with digital certificate. <- Prev: Send email with embedded images | Next: Email encryption (S/MIME) - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Email Encryption in C# - S/MIME

Email Encryption in C# - S/MIME


In previous section, I introduced how to send email with digital signature. In this section, I will introduce how to encrypt email with digital certificate in C#. After the recipient received your email with digital signature, the recipient can get your digital certificate public key from your digital signature. Then the recipient can encrypt an email with your public key and send it to you. Only you can decrypt this email with your private key. That is how S/MIME can protect your email content. If you don't expose your digital certificate private key to others, none can read your email which is encrypted by your public key. If you received an email with digital signature, your email client usually stores the public key of the sender in "Control Panel" -> "Internet Options" -> "Content" -> "Certificates" -> "Other People". Then you can use the following code to encrypt email and send it to your recipient. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Email Encryption (S/MIME)]


The following example codes demonstrate how to use EASendMail SMTP component to encrypt email with digital certificate. To get the full samples of EASendMail, please refer to Samples section.

using using using using

System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test encrypted email"; // Set email body oMail.TextBody = "this is a test email with email encryption"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); //User and password for ESMTP authentication, if your server doesn't require //User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; try { // Find certificate by email adddress in My Personal Store. // Once the certificate is loaded to From, the email content // will be signed automatically oMail.From.Certificate.FindSubject(oMail.From.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "My"); } catch (Exception exp) { Console.WriteLine("No sign certificate found for <" + oMail.From.Address + ">:" + exp.Message); } int count = oMail.To.Count; for (int i = 0; i < count; i++) { MailAddress oAddress = oMail.To[i] as MailAddress; try { // Find certificate by email adddress in My Other Peoples Store.

// The certificate can be also imported by *.cer file like this: // oAddress.Certificate.Load("c:\\encrypt1.cer"); // Once the certificate is loaded to MailAddress, the email content // will be encrypted automatically oAddress.Certificate.FindSubject(oAddress.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "AddressBook"); } catch (Exception ep) { try { oAddress.Certificate.FindSubject(oAddress.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "My"); } catch (Exception exp) { Console.WriteLine("No encryption certificate found for <" + oAddress.Address + ">:" + exp.Message); } } } // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send encrypted email ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

Send Email in C# - Tutorial


Introduction EASendMail is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). This tutorial covers everything of sending email with EASendMail in C#.

y y y y y y y y y y y y y y y

A simple C# project Send email over SSL connection Send email using Gmail account Send email using Yahoo account Send email using Hotmail/MSN Live account Send email directly without SMTP server(MX DNS lookup) Send HTML email Send email with attachment Send email with embedded images Send email with digital signature (S/MIME) Email encryption (S/MIME) Send email with event handler Send email in asynchronous mode Send email with multiple threads (Mass Mail) Total sample projects

Installation Before you can use the following sample codes, you should download the EASendMail Installer and install it on your machine at first.

A simple C# project
To better demonstrate how to use EASendMail sending email, let's create a C# console project at first, and then add the reference of EASendMail in your project.

Add Reference of EASendMail to Visual Stuido C#.NET Project

To use EASendMail SMTP Component in your project, the first step is "Add reference of EASendMail to your project". Please create/open your project with Visual Studio.NET, then choose menu->"Project"->"Add Reference"->".NET"->"Browse...", and choose the EASendMail{version}.dll from your disk, click "Open"->"OK", the reference of EASendMail will be added to your project, and you can start to use EASendMail to send email in your project.

Because EASendMail has separate builds for .Net Framework, please refer to the following table and choose the correct dll. Separate builds of run-time assembly for .Net Framework 1.1, 2.0, 3.5, 4.0 and .Net Compact Framework 2.0, 3.5. File EASendMail.dll .NET Framework Version Built with .NET Framework 1.1 It requires .NET Framework 1.1, 2.0, 3.5 or later version. Built with .NET Framework 2.0 It requires .NET Framework 2.0, 3.5 or later version. Built with .NET Framework 3.5 It requires .NET Framework 3.5 or later version. Built with .NET Framework 4.0 It requires .NET Framework 4.0 or later version. Built with .NET Compact Framework 2.0 It requires .NET Compact Framework 2.0, 3.5 or later version. Built with .NET Compact Framework 3.5 It requires .NET Compact Framework 3.5 or later

EASendMail20.dll

EASendMail35.dll EASendMaill40.dll

EASendMailCF20.dll

EASendMailCF35.dll

version.

[C# Example - Send email]


Now add the following codes to the project and change From, To, Server, User and Password to corresponding value.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email from c# project"; // Set email body oMail.TextBody = "this is a test email sent from c# project, do not reply"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send email ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:");

Console.WriteLine(ep.Message); } } } }

If you set everything right, you can get "email was sent successfully". If you get "failed to send email with the following error:", then please have a look at the following section. Where can I get my SMTP email server address, user and password? Because each email account provider has different server address, so you should query your SMTP server address from your email account provider. To prevent spreading email from the server, most SMTP servers also require user authentication. User name is your email address or your email address without domain part, it depends on your email provider setting. When you execute above example code, if you get error about "Networking connection" or "No such host", it is likely that your SMTP server address is not correct. If you get an error like "5xx Relay denied", it is likely that you did not set user authentication. Another common error is "5xx Must issue a STARTTLS command first" or "No supported authentication marshal found!", that is because your SMTP server requires user authentication under SSL connection. You can set the SSL connection to solve this problem. Finally, if you have already set your account in your email client such as Outlook or Window Mail, you can query your SMTP server address, user in your email client. For example, you can choose menu -> "Tools" - > - "Accounts" - > "Your email account" - > "Properties" - > "Servers" in Outlook express or Windows Mail to get your SMTP server, user. Using EASendMail to send email does not require you have email client installed on your machine or MAPI, however you can query your exist email accounts in your email client.

Email Address Syntax and Multiple Recipients Mail Address Syntax in EASendMail SMTP Component: For single email address (From, ReplyTo, ReturnPath), the syntax can be: ["][display name]["]<email address>. For example: "Tester, T" <test@adminsystem.com>, Tester <test@adminsystem.com>, <test@adminsystem.com> or test@adminsystem.com. For mulitple email address (To, CC, Bcc), the syntax can be: [single email],[single email]... (,;\r\n) can be used to separate multiple email addresses. For example: "Tester, T" <test1@adminsystem.com>, Tester2 <test2@adminsystem.com>, <test3@adminsystem.com>, test4@adminsystem.com

[C# Example - Email syntax]


To better understand the email address syntax, please refer to the following codes.
// From is a MailAddress object, it supports implicit converting from string. // The syntax is like this: "test@adminsystem.com" or "Tester<test@adminsystem.com>" // The example code without implicit converting. oMail.From = new MailAddress( "Tester", "test@adminsystem.com" ); oMail.From = new MailAddress( "Tester<test@adminsystem.com>"); oMail.From = new MailAddress( "test@adminsystem.com" ); // To, Cc and Bcc is a AddressCollection object, it supports implicit converting // from string. Multiple addresses are separated with (,;) // The syntax is like this: "test@adminsystem.com, test1@adminsystem.com" // The example code without implicit converting oMail.To = new AddressCollection( "test1@adminsystem.com, test2@adminsystem.com" ); oMail.To = new AddressCollection( "Test1<test@adminsystem.com>, Test2<test2@adminsystem.com>"); // You can add more recipient by Add method oMail.To.Add( new MailAddress( "tester", "test@adminsystem.com")); // You can also add carbon copy (CC) or blind carbon copy (BCC) in the email. oMail.Cc.Add( new MailAddress( "CC recipient", "cc@adminsystem.com")); oMail.Bcc.Add( new MailAddress( "Bcc recipient", "bcc@adminsystem.com"));

Reply-To, Return-Path and Mail Priority If you want to set another email address to get the replied email rather than your From address, you can use ReplyTo property. If you want to set another email address to get the delivery report rather than your From address, you can use ReturnPath property. If you want to set Higher or Lower priority to your email, you can use Priority prority

[C# Example - ReplyTo, ReturnPath and Priority]


oMail.From = "Tester <test@emailarchitect.net>"; // Set the Reply-To address oMail.ReplyTo = "replyto@@emailarchitect.net"; // Set the email address to receive delivery report oMail.ReturnPath = "report@emailarchitect.net"; // Set high priority oMail.Priority = MailPriority.High;

Next Section In this section, I introduced the basic things of sending email in C# with EASendMail. At next section I will introduce how to send email over SSL connection. Next: Send email over SSL in C# -> Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Send Email with Digital Signature in C# - S/MIME

Send Email with Digital Signature in C# S/MIME


In previous section, I introduced how to send email with embedded images. In this section, I will introduce how to sign email with digital certificate in C#. Digital signature prevents email content is faked or changed in transport level. Encrypting email protects email content from exposure to inappropriate recipients. Both digital signature and email encrypting depend on digital certificate. If you have an email digital signature certificate installed on your machine, you can find it in "Control Panel" -> "Internet Options" -> "Content" -> "Certificates" -> "Personal".

Then you can use your email certificate to sign the email by the following code. If you don't have a certificate for your email address, you MUST get a digital certificate for personal email protection from third-party certificate authorities such as www.verisign.com. If you need a free certificate for your email address, you can go to http://www.comodo.com/home/email-security/free-email-certificate.php to apply for one year free email certificate. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email with digital signature (S/MIME)]


The following example codes demonstrate how to use EASendMail SMTP component to sign email with digital certificate. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program {

static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email with digital signature"; // Set email body oMail.TextBody = "this is a test email with digital signature"; // Your smtp server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; try { // Find certificate by email adddress in My Personal Store. // Once the certificate is loaded to From, the email content // will be signed automatically oMail.From.Certificate.FindSubject(oMail.From.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "My"); } catch (Exception exp) { Console.WriteLine("No sign certificate found for <" + oMail.From.Address + ">:" + exp.Message); } // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send email with digital signature ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } }

} }

Next Section At next section I will introduce how to encrypt email with digital certificate. <- Prev: Send email with embedded images | Next: Email encryption (S/MIME) - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Email Encryption in C# - S/MIME

Email Encryption in C# - S/MIME


In previous section, I introduced how to send email with digital signature. In this section, I will introduce how to encrypt email with digital certificate in C#. After the recipient received your email with digital signature, the recipient can get your digital certificate public key from your digital signature. Then the recipient can encrypt an email with your public key and send it to you. Only you can decrypt this email with your private key. That is how S/MIME can protect your email content. If you don't expose your digital certificate private key to others, none can read your email which is encrypted by your public key. If you received an email with digital signature, your email client usually stores the public key of the sender in "Control Panel" -> "Internet Options" -> "Content" -> "Certificates" -> "Other People". Then you can use the following code to encrypt email and send it to your recipient. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Email Encryption (S/MIME)]


The following example codes demonstrate how to use EASendMail SMTP component to encrypt email with digital certificate. To get the full samples of EASendMail, please refer to Samples section.
using System; using System.Collections.Generic;

using System.Text; using EASendMail; //add EASendMail namespace namespace mysendemail { class Program { static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test encrypted email"; // Set email body oMail.TextBody = "this is a test email with email encryption"; // Your SMTP server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); //User and password for ESMTP authentication, if your server doesn't require //User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; try { // Find certificate by email adddress in My Personal Store. // Once the certificate is loaded to From, the email content // will be signed automatically oMail.From.Certificate.FindSubject(oMail.From.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "My"); } catch (Exception exp) { Console.WriteLine("No sign certificate found for <" + oMail.From.Address + ">:" + exp.Message); } int count = oMail.To.Count; for (int i = 0; i < count; i++) { MailAddress oAddress = oMail.To[i] as MailAddress; try { // Find certificate by email adddress in My Other Peoples Store. // The certificate can be also imported by *.cer file like this:

// oAddress.Certificate.Load("c:\\encrypt1.cer"); // Once the certificate is loaded to MailAddress, the email content // will be encrypted automatically oAddress.Certificate.FindSubject(oAddress.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "AddressBook"); } catch (Exception ep) { try { oAddress.Certificate.FindSubject(oAddress.Address, Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, "My"); } catch (Exception exp) { Console.WriteLine("No encryption certificate found for <" + oAddress.Address + ">:" + exp.Message); } } } // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send encrypted email ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

If you received digital signed and encrypted email by Windows Mail(Outlook Express), it looks like this:

Next Section At next section I will introduce how to send email with event handler. <- Prev: Send email with digtial signature - S/MIME | Next: Send email with event handler > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

Send Email with Event Handler in C#


In previous section, I introduced how to encrypt email with digital certificate. In this section, I will introduce how to send email with event handler in C#. In previous examples, after SendMail method is invoked, if you want to know the progress of the email sending, you should use Event Handler. The following sample codes demonstrate how to use Event Handler to monitor the progress of email sending.

Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email with event handler]


The following example codes demonstrate how to use EASendMail SMTP component to send email with event handler. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { public static void OnSecuring( object sender, ref bool cancel ) { Console.WriteLine("Securing ... "); } public static void OnAuthorized( object sender, ref bool cancel ) { Console.WriteLine("Authorized"); } public static void OnIdle( object sender, ref bool cancel ) { // this event is fired when the SmtpClient is wait for response from // smtp server, if you add Application.DoEvents in windows form application, // it can prevent your form has no response before SendMail is not returned. // Application.DoEvents(); } public static void OnConnected( object sender, ref bool cancel ) { Console.Write("Connected\r\n"); } public static void OnSendingDataStream(

object sender, int sent, int total, ref bool cancel ) { Console.WriteLine(String.Format("{0}/{1} sent", sent, total)); } static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email from c# with event handler"; // Set email body oMail.TextBody = "this is a test email sent from c# project with event handler"; // Your smtp server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; // Add event handlers to current SmtpClient instance. oSmtp.OnAuthorized += new SmtpClient.OnAuthorizedEventHandler( OnAuthorized ); oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler( OnIdle ); oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler( OnConnected ); oSmtp.OnSecuring += new SmtpClient.OnSecuringEventHandler( OnSecuring ); oSmtp.OnSendingDataStream += new SmtpClient.OnSendingDataStreamEventHandler( OnSendingDataStream ); try { Console.WriteLine("start to send email ..."); oSmtp.SendMail(oServer, oMail); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) {

Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

Next Section At next section I will introduce how to send email in asynchronous mode. <- Prev: Email encryption - S/MIME | Next: Send email in asynchronous mode- > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

Send Email with Asynchronous Mode in C#


In previous section, I introduced how to use event handler to monitor the progress. In this section, I will introduce how to send email in asynchronous mode in C#. In synchronous mode, once SendMail method is called, it returns to application after the method is complete. Therefore, if the runtime (it depends on the networking connection and the email size) is long, your application cannot do anything before this method ends, which results "my application is blocked or halted". In contrast, in asynchronous mode, as BeginSendMail method works in background, this methods return to application immediately no matter the running method is complete or not. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send email in asynchronous mode]


The following example codes demonstrate how to use EASendMail SMTP component to send email in asynchronous mode. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program

{ static void Main(string[] args) { SmtpMail oMail = new SmtpMail("TryIt"); SmtpClient oSmtp = new SmtpClient(); // Set sender email address, please change it to yours oMail.From = "test@emailarchitect.net"; // Set recipient email address, please change it to yours oMail.To = "support@emailarchitect.net"; // Set email subject oMail.Subject = "test email from c# with asynchronous mode"; // Set email body oMail.TextBody = "this is a test email sent from c# project with asynchronous mode"; // Your smtp server address SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; // If your smtp server requires SSL connection, please add this line // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; try { Console.WriteLine("start to send email in asynchronous mode..."); SmtpClientAsyncResult oResult = oSmtp.BeginSendMail( oServer, oMail, null, null); // Wait for the email sending... while (!oResult.IsCompleted) { Console.WriteLine("waiting..., you can do other thing!"); oResult.AsyncWaitHandle.WaitOne(50, false); } oSmtp.EndSendMail(oResult); Console.WriteLine("email was sent successfully!"); } catch (Exception ep) { Console.WriteLine("failed to send email with the following error:"); Console.WriteLine(ep.Message); } } } }

Next Section

At next section I will introduce how to send mass email with multiple threads. <- Prev: Send email with event handler | Next: Send email with multiple threads(mass mail)> Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

Send Email with Multiple Threads(Mass Mail) in C#


In previous section, I introduced how to use asynchronous mode. In this section, I will introduce how to send mass email with multiple threads in C#. Based on asynchronous mode, you can create multiple SmtpClient instances in your application and send email in multiple threads. Here is a simple sample demonstrates how to use asynchronous mode to send email in multiple threads. Remarks: All of samples in this section are based on first section: A simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# Example - Send mass email with multiple threads]


The following example codes demonstrate how to use EASendMail SMTP component to send mass email with multiple threads. To get the full samples of EASendMail, please refer to Samples section.
using using using using System; System.Collections.Generic; System.Text; EASendMail; //add EASendMail namespace

namespace mysendemail { class Program { static void Main(string[] args) { string[] arRcpt = new string[]{"test1@adminsystem.com", "test2@adminsystem.com", "test3@adminsystem.com" }; 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 = "sender@emailarchitect.net"; // 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.emailarchitect.net"); // User and password for ESMTP authentication, if your server doesn't require // User authentication, please remove the following codes. oServer.User = "test@emailarchitect.net"; oServer.Password = "testpassword"; // 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++; } } } } }

See Also <- Prev: Send email with asynchronous mode | Next: Total email sending sample projects - > Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

EASendMail SMTP Component > Developer Center > Total Email Sample Projects - SMTP

Total Email Sample Projects - SMTP


After you downloaded the EASendMail SMTP Component Installer and install it on your machine, there are many samples in the installation path. asp Send email from ASP (VBScript, JScript) -

ActiveX/COM asp_queue Send email from ASP to EASendMail Service. (VBScript, JScript) ActiveX/COM Send email from ASP to EASendMail Service, EASendMail service will select recipients from database. (VBScript, JScript) - ActiveX/COM Send email from ASP.NET. (C#, VB, JScript.NET) Send bulk emails with multiple threads from ASP.NET. (C#, VB) Send email from ASP.NET to EASendMail Service. (C#, VB, JScript.NET) Send email from ASP.NET to EASendMail Service, EASendMail service will select recipients from database. (C#, VB, JScript.NET) Send text/plain email from Visual Basic 6.0. This sample also demonstrates digital signature, email encryption. (VB6) ActiveX/COM Send text/plain email from Delphi. This sample also demonstrates digital signature, email encryption. (Delphi) - ActiveX/COM Send text/plain email from Visual C++. This sample also demonstrates digital signature, email encryption. (Visual C++) ActiveX/COM Send text/plain email from Visual Basic.NET. This sample also demonstrates digital signature, email encryption. Send text/plain email from C#. This sample also demonstrates digital signature, email encryption. Send text/plain email from managed c++. This sample also demonstrates digital signature, email encryption.

asp_queue_database

asp_net asp_net_batch asp_net_queue

asp_net_queue_database

simple.vb6

simple.delphi

simple.vcNative

simple.vb

simple.csharp

simple.vc

htmlmail.vb6

Send text/html email from Visual Basic 6.0. This sample also demonstrates embedded pictures, digital signature, email encryption. (VB6) - ActiveX/COM Send text/html email from Delphi. This sample also demonstrates embedded pictures, digital signature, email encryption. (Delphi) - ActiveX/COM Send text/html email from Visual C++. This sample also demonstrates embedded pictures, digital signature, email encryption. (Visual C++) - ActiveX/COM Send text/html email from Visual Basic.NET. This sample also demonstrates embedded pictures, digital signature, email encryption. Send text/html email from C#. This sample also demonstrates embedded pictures, digital signature, email encryption. Send email by FastSender with multiple threadings. This sample also demonstrates email address validating. (VB6) ActiveX/COM Send email by FastSender with multiple threadings. This sample also demonstrates email address validating. (Delphi) ActiveX/COM Send email by BeginSendMail with multiple threadings. This sample also demonstrates email address validating. Send email by BeginSendMail with multiple threadings. This sample also demonstrates email address testing. Send email from PocketPC/Windows Mobile System. Send email from PocketPC/Windows Mobile System.

htmlmail.delphi

htmlmail.vcNative

htmlmail.vb

htmlmail.csharp

mass.vb6

mass.delphi

mass.vb

mass.csharp

samples_vs2008\pocketpc.mobile.cs samples_vs2008\pocketpc.mobile.vb

Free Email Support

Not enough? Please contact our technical support team. Support@EmailArchitect.NET Remarks We usually reply emails in 24hours. The reason for getting no response is likely that your smtp server bounced our reply. In this case, please try to use another email address to contact us. Your Gmail, Hotmail or Yahoo email account is recommended. See Also <- Prev: Tutorial Index Expand All Sections - >> Comments If you have any comments or questions about above example codes, please click here to add your comments.

You might also like