Sending Emails Through SSL SMTP with .NET Framework
Many email servers require SSL connections for secure email sending. Here's how you can achieve this in .NET Framework:
The Issue:
Sending emails through an SSL SMTP server on port 465 using the default approach often results in timeouts due to implicit SSL support issues in System.Net.Mail.
The Solutions:
1. Using GMail's SMTP Server:
If your email server supports GMail's SMTP settings, you can utilize their SSL SMTP server and adjust the code snippet you provided:
<code class="csharp">using System.Web.Mail; using System; //... SmtpMail.SmtpServer = "smtp.gmail.com:465";</code>
2. CDO Library for Custom SSL Settings:
You can use the Microsoft Collaborative Data Objects (CDO) library to configure custom SSL settings:
<code class="csharp">using System.Web.Mail; using System; using System.Web.Mail; //... myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "<Your SMTPO Server>"); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "<Your SMTP Port>");</code>
3. Explicit STARTTLS:
For servers that support Explicit STARTTLS (port 587), use the following code:
<code class="csharp">using System.Net.Mail; using System; //... _SmtpServer.EnableSsl = true; // Ensure encryption System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;</code>
By configuring custom SSL settings or utilizing GMail's SMTP server, you can successfully send emails through SSL SMTP with the .NET Framework.
The above is the detailed content of How to Send Emails Securely Through SSL SMTP with .NET Framework?. For more information, please follow other related articles on the PHP Chinese website!