Sending Email in C Isharuye
Sending Email in C Isharuye
Sending Email in C Isharuye
cat=14
EASendMail SMTP Component > Developer Center > Send Email over SSL in C#
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.
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); } } } }
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#
[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#
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#
[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#
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#
{ 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#
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#
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.
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
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.
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
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); } } } }
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.
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.
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
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
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
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.
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
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.
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.
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) {
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.
{ 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.
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
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_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
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.