次の方法で共有


System.Net.Mail cannot send secured email using SMTP over SSL aka SMTPS protocol

This is just FYI that we know the limitation of System.Net.Mail classes and its incapability of sending secured emails over SSL / SMTPS.

The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.

An alternate connection method is where an SSL session is established up front before any protocol commands are sent. This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

This is the limitation of SmtpClient and publically documented here but often missed

https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx

You may also ask your service provider to see if they can support STARTTLS sessions, which is fairly secure and not too complex to implement.

You have an option to use either CDOSYS / System.Web.Mail from .Net 1.1 which internally uses CDOSYS to send out email. CDOSYS had this support but then the support was dropped in System.Net.Mail. Some may argue that why this downgrade, when CDOSYS had this support why System.Net.Mail drop this support – well this is not really a downgrade but a design decision that they made to support only one of the two optional ways to implement SSL while the SMTPS is used less commonly when compared to TLS.

Here is the sample code that can do the job for you, please do not forget to add a reference to System.Web.dll into your project for this to work. I have compiled it on VS2008 and found it working.

Please do proper testing before using this code in production and treat this piece of code just for the sake of educational purpose.

 using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mail;
 
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
 
            MailMessage msg = new MailMessage();
            msg.To = "to@server.com";
            msg.From = "from@server.com";
            msg.Subject = "This is a test email using System.Web.Mail";
            msg.Body = "Hello There!!!";
            msg.BodyFormat = MailFormat.Text;
 
            // SMTP Server to use
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.server.com");   
 
            // SMTP Port to use
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");                 
            
            // Send using Value, leave it to 2 for sending via SMTP Port specified
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendusing", "2");                        
            
            // Use SSL : 0 = False, 1 = True
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");                       
 
 
            // Authentication mode: 
            //  0 - Anonymous
            //  1 - Basic
            //  2 - NTLM
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
 
 
            // Username and password to connect to the SMTP Server
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendusername", "USERNAME_HERE");
            msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendpassword", "PASSWORD_HERE");
 
            SmtpMail.Send(msg);            
 
        }
    }
}