Radu D
Radu D

Reputation: 3555

C# send email using implicit ssl

Is there any library preferable free that can be used in order to sens email using implicit ssl protocol. My hosting provider support ssl emails ... but standard .net email client cannot handle that.

Upvotes: 4

Views: 16882

Answers (5)

Luke
Luke

Reputation: 841

System.Net.Mail does support "explicit SSL" (also known as "StartTLS" - usually on port 25 or 587), but not "implicit SSL" (aka "SMTPS" - usually on port 465).

As far as I know, explicit SSL starts from an unsecured connection, then the STARTTLS command is given and finally a SSL secured connection is made. Implicit SSL, on the other side, requires that the SSL connection is set up before the two parties start talking.

Some servers (like gmail) accept both, so you simply need to set EnableSsl to true and send to the right port. If your server does not support explict SSL, though, this "simple way" is not an option.

I'm also still looking around for a general solution for using System.Net.Mail with implicit SSL, with no luck so far.

Anyway take a look at this article, it may give you some insight.

[edit: @Nikita is right, fixed port numbers to avoid confusion]

Upvotes: 4

SLdragon
SLdragon

Reputation: 1627

You can use AIM (Aegis Implicit Mail) to send email through implicit SSL:

  1. First install the package: Install-Package AIM
  2. Then Use the sample code to send email

    class Mail
    {
        private static string mailAddress = "{you email address}";
        private static string host = "{your host server}";
        private static string userName = "{your user name}";
        private static string password = "{your password}";
        private static string userTo = "{to address}";
        private static void SendEmail(string subject, string message)
        {
            //Generate Message 
            var mailMessage = new MimeMailMessage();
            mailMessage.From = new MimeMailAddress(mailAddress);
            mailMessage.To.Add(userTo);
            mailMessage.Subject = subject;
            mailMessage.Body = message;
    
            //Create Smtp Client
            var mailer = new MimeMailer(host, 465);
            mailer.User = userName;
            mailer.Password = password;
            mailer.SslType = SslMode.Ssl;
            mailer.AuthenticationMode = AuthenticationType.Base64;
    
            //Set a delegate function for call back
            mailer.SendCompleted += compEvent;
            mailer.SendMailAsync(mailMessage);
        }
    
        //Call back function
        private static void compEvent(object sender, AsyncCompletedEventArgs e)
        {
            if (e.UserState != null)
                Console.Out.WriteLine(e.UserState.ToString());
    
            Console.Out.WriteLine("is it canceled? " + e.Cancelled);
    
            if (e.Error != null)
                Console.Out.WriteLine("Error : " + e.Error.Message);
        }
    }
    

Upvotes: 0

durbo
durbo

Reputation: 566

Use the TLS port (ie 587) rather than the SSL port. I had the same issue for months until I found this solution.

Sending email in .NET through Gmail

Upvotes: 3

As one of options, our SecureBlackbox includes SMTP component which works via both implicit and explicit SSL and supports different authentication mechanisms (including SASL, NTLM etc).

Upvotes: 0

Bruno
Bruno

Reputation: 122719

You may still be able to use the deprecated System.Web.Mail.MailMessage API (and set its "http://schemas.microsoft.com/cdo/configuration/smtpusessl" option, for explicit SSL/TLS):

System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
// ...
mailMsg.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 true);

Alternatively, if you can, you could run something like stunnel locally to establish an SSL/TLS tunnel from your localhost to your SMTP server. Then, you would have to connect normally (without SSL/TLS) to the tunnel's localhost end as your SMTP server.

Upvotes: 1

Related Questions