In previous section, I introduced the basic things of email sending in C#. In this section, I will introduce the SSL connection.
Sections:
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:
Explicit SSL (TLS)
Using STARTTLS command to switch SSL channel on normal SMTP port (25 or 587);
Implicit SSL
Deploying SSL on another port (465 or other port, you may query it from your server administrator
EASendMail SMTP component supports both ways. The connection can be specified by EASendMail.SmtpConnectType enumeration.
TLS is the successor of SSL, more and more SMTP servers require TLS 1.2 encryption now.
If your operating system is Windows XP/Vista/Windows 7/Windows 2003/2008/2008 R2/2012/2012 R2
,
and you got connection error with SSL/TLS connection,
you need to enable TLS 1.2 protocol in your operating system like this:
Enable TLS 1.2 on Windows XP/Vista/7/10/Windows 2008/2008 R2/2012
// 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;
Note
Remarks: All of samples in this section are based on first section: Send email in A simple C# XAML Windows Store App 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.
The following example codes demonstrate how to use EASendMail SMTP component to send email with direct SSL connection on 465 port.
Note
To get the full sample projects, please refer to Samples section.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// Add EASendMail and Tasks Namespace
using System.Threading.Tasks;
using EASendMail;
namespace CSharp_Windows_Store_App
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private async void btnSend_Click(object sender, RoutedEventArgs e)
{
btnSend.IsEnabled = false;
await Send_Email();
btnSend.IsEnabled = true;
}
private async Task Send_Email()
{
String Result = "";
try
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Set sender email address, please change it to yours
oMail.From = new MailAddress("test@emailarchitect.net");
// Add recipient email address, please change it to yours
oMail.To.Add(new MailAddress("support@emailarchitect.net"));
// Set email subject and email body text
oMail.Subject = "test email from C# XAML project";
oMail.TextBody = "this is a test email sent from Windows Store App, do not reply";
// Your SMTP server address
SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");
// User and password for ESMTP authentication
oServer.User = "test@emailarchitect.net";
oServer.Password = "testpassword";
// SMTP server requires SSL connection on 465 port, please add this line
oServer.Port = 465;
Server.ConnectType = SmtpConnectType.ConnectSSLAuto;
await oSmtp.SendMailAsync(oServer, oMail);
Result = "Email was sent successfully!";
}
catch (Exception ep)
{
Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
}
// Display Result by Diaglog box
Windows.UI.Popups.MessageDialog dlg = new
Windows.UI.Popups.MessageDialog(Result);
await dlg.ShowAsync();
}
}
}
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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// Add EASendMail and Tasks Namespace
using System.Threading.Tasks;
using EASendMail;
namespace CSharp_Windows_Store_App
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private async void btnSend_Click(object sender, RoutedEventArgs e)
{
btnSend.IsEnabled = false;
await Send_Email();
btnSend.IsEnabled = true;
}
private async Task Send_Email()
{
String Result = "";
try
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Set sender email address, please change it to yours
oMail.From = new MailAddress("test@emailarchitect.net");
// Add recipient email address, please change it to yours
oMail.To.Add(new MailAddress("support@emailarchitect.net"));
// Set email subject and email body text
oMail.Subject = "test email from C# XAML project";
oMail.TextBody = "this is a test email sent from Windows Store App, do not reply";
// Your SMTP server address
SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");
// User and password for ESMTP authentication
oServer.User = "test@emailarchitect.net";
oServer.Password = "testpassword";
// Enable TLS connection on 25 port, please add this line
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
await oSmtp.SendMailAsync(oServer, oMail);
Result = "Email was sent successfully!";
}
catch (Exception ep)
{
Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
}
// Display Result by Diaglog box
Windows.UI.Popups.MessageDialog dlg = new
Windows.UI.Popups.MessageDialog(Result);
await dlg.ShowAsync();
}
}
}
Next Section
At next section I will introduce how to send email using Gmail account.
Appendix
Comments
If you have any comments or questions about above example codes, please click here to add your comments.