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#.
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#.
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.
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.