
Simple Mail Transfer Protocol (SMTP) is the most commonly used email protocol that deals with sending email messages from client applications to the email server. In various cases, the email clients are implemented within .NET applications in order to send emails via SMTP. For such scenarios, this article covers how to connect to an SMTP server programmatically using C#. Furthermore, the code samples demonstrate how to send an email via SMTP client after connection establishment.
C# API to Connect SMTP Server
Aspose.Email for .NET is a C# API that is designed to create powerful email client applications. The API makes it possible to perform complex email operations quite easily within a few lines of code. Furthermore, it allows you to connect SMTP servers via SOCKS and HTTP proxy servers. You can either download the API’s DLL or install it using NuGet.
PM> Install-Package Aspose.Email
Connect to SMTP Server using C#
Before connecting to an SMTP server, you need to have the following things known.
- Username
- Password
- Port
Once you have them, you can configure the SMTP client using the following steps.
- Create an object of SmtpClient class.
- Set username, password, and port number using SmtpClient.Username, SmtpClient.Password, and SmtpClient.Port properties respectively.
- Set Security options using SmtpClient.SecurityOptions property.
The following code sample shows how to set up the API for an SMTP connection in C#.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
SmtpClient client = new SmtpClient("smtp.gmail.com"); | |
// Set username, password, port, and security options | |
client.Username = "your.email@gmail.com"; | |
client.Password = "your.password"; | |
client.Port = 587; | |
client.SecurityOptions = SecurityOptions.SSLExplicit; |
Once you have configured the API, you can connect to an SMTP server using one of the following options.
Connect to SMTP via SOCKS Proxy Server in C#
Aspose.Email provides support for versions 4, 4a, and 5 of the SOCKS proxy protocol. The following are the steps to connect to an SMTP server via a SOCKS proxy server using Aspose.Email for .NET.
- Initialize SmtpClient with the host address, username, password, and other settings.
- Create an object of the SocksProxy class and set the address and port.
- Set the client’s proxy using SmtpClient.Proxy property.
- Send email using SmtpClient.Send() method.
The following code sample shows how to connect to an SMTP server via SOCKS proxy using C#.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
SmtpClient client = new SmtpClient("smtp.domain.com", "username", "password"); | |
client.SecurityOptions = SecurityOptions.SSLImplicit; | |
string proxyAddress = "192.168.203.142"; // proxy address | |
int proxyPort = 1080; // proxy port | |
SocksProxy proxy = new SocksProxy(proxyAddress, proxyPort, SocksVersion.SocksV5); | |
client.Proxy = proxy; | |
client.Send(new MailMessage("sender@domain.com", "receiver@domain.com", "Sending Email via proxy", "Implement socks proxy protocol for versions 4, 4a, 5 (only Username/Password authentication)")); |
Connect to SMTP Server via HTTP Proxy Server
The following are the steps to connect to an SMTP server via an HTTP proxy server.
- Create an instance of the HttpProxy class and initialize it with IP and port number.
- Create and configure the SmtpClient object.
- Set HTTP proxy using SmtpClient.Proxy property.
- Send email using SmtpClient.Send() method.
The following code sample shows how to connect to an SMTP server via HTTP proxy server.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
HttpProxy proxy = new HttpProxy("18.222.124.59", 8080); | |
using (SmtpClient client = new SmtpClient("host", 587, "username", "password")) | |
{ | |
client.Proxy = proxy; | |
client.Send(new MailMessage( | |
"from@domain.com", | |
"to@domain.com", | |
"NETWORKNET-34226 - " + Guid.NewGuid().ToString(), | |
"NETWORKNET-34226 Implement socks proxy protocol for versions 4, 4a, 5 (only Username/Password authentication)")); | |
} |
Get a Free API License
You can try and use Aspose.Email for .NET for free by getting a temporary license.
Conclusion
In this article, you have learned how to connect to an SMTP server using C#. Furthermore, the step-by-step guide and code samples have shown how to connect to the SMTP server via SOCKS and HTTP proxy servers. You can explore more about the C# email API using documentation.