четверг, 27 сентября 2012 г.

C# шлем email, используя TLS и SSL

Рассказывать ( как всегда много не буду)
Просто пример рабочего кода, где шлется сообщение на gmail используя TLS, к сожалению System.Net.Mail.SmtpClient не шлет сообщения используя SSL протокол через порт 465.

using System;
using System.Net;
using System.Windows.Forms;
using System.Net.Mail;

namespace Mailer
{

        private void Send()
        {
            //Создаем сообщение
            MailMessage message = new MailMessage("from_email@gmail.com", "to_email@gmail.com");
            message.Subject = "Test";
            message.Body = "Test";
            
            //Создаем клиента для отправки
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

            //Логин+пароль
            NetworkCredential credentials = new NetworkCredential(
                "login",
                "password");
            
           
            smtpClient.Credentials = credentials;
            //Включаем SSL
            smtpClient.EnableSsl = true;
            //Посылаем
            smtpClient.Send(message);
        }
}

А вот что бы слать с SSL можно воспользоватся компонентом CDO
Вот здесь можно прочитать подробный мануал http://support.microsoft.com/kb/310212.
Ну а я представляю кусок кода


 private void Send2()
        {
                string yourEmail = "..............@gmail.com";

                CDO.Message message = new CDO.Message();
                CDO.IConfiguration configuration = message.Configuration;
                ADODB.Fields fields = configuration.Fields;

                Console.WriteLine(String.Format("Configuring CDO settings..."));

                // Set configuration.
                // sendusing:               cdoSendUsingPort, value 2, for sending the message using the network.
                // smtpauthenticate:     Specifies the mechanism used when authenticating to an SMTP service over the network.
                //                                  Possible values are:
                //                                  - cdoAnonymous, value 0. Do not authenticate.
                //                                  - cdoBasic, value 1. Use basic clear-text authentication. (Hint: This requires the use of "sendusername" and "sendpassword" fields)
                //                                  - cdoNTLM, value 2. The current process security context is used to authenticate with the service.

                ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
                field.Value = "smtp.gmail.com";

                field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
                field.Value = 465;

                field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                field.Value = CDO.CdoSendUsing.cdoSendUsingPort;

                field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
                field.Value = CDO.CdoProtocolsAuthentication.cdoBasic;

                field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
                field.Value = yourEmail;

                field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
                field.Value = "**********";

                field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
                field.Value = "true";

                fields.Update();

                Console.WriteLine(String.Format("Building CDO Message..."));

                message.From = yourEmail;
                message.To = yourEmail;
                message.Subject = "Test message.";
                message.TextBody = "This is a test message. Please disregard.";

 

                // Send message.
                message.Send();
        }