Загрузка...

Отправка почты посредством Smtp сервером

Тема в разделе C# создана пользователем r3xq1 1 авг 2018. 236 просмотров

Загрузка...
  1. r3xq1
    r3xq1 Автор темы 1 авг 2018 https://t.me/DarkSoft_Extra 119 27 июл 2018
    Код
    using System;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;

    namespace SmtpServer
    {
    public class SettingMessage
    {
    public static void SendToMail(string Login, string Password, string ToSend, string Logo, string SmtpServer, int Port, int Timeout)
    {
    // var AttachFiles = new List<string>(); // Сюда копируем файлы ( логику придумайте сами ).
    using (var mess = new MailMessage())
    {
    mess.From = new MailAddress(Login);
    mess.To.Add(new MailAddress(ToSend));
    mess.Subject = Environment.UserName;
    mess.BodyEncoding = Encoding.UTF8;
    mess.SubjectEncoding = Encoding.UTF8;
    mess.Body = Logo;
    mess.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    /*
    foreach (var Files in AttachFiles)
    {
    mess.Attachments.Add(new Attachment(Files)); // Добавить файлы для отправки
    }
    */
    using (var client = new SmtpClient(SmtpServer, Convert.ToInt16(Port)))
    {
    client.EnableSsl = true;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Timeout = Timeout;
    client.Credentials = new NetworkCredential(Login, Password);
    ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
    try
    {
    client.Send(mess);
    Console.WriteLine($"Сообщение успешно отправлено на адресс: {ToSend}");
    //AttachFiles.Clear();
    }
    catch (SmtpException ex) { File.AppendAllText("SmtpServerError.txt", ex.Message); }
    }
    }
    }

    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
    {
    if (error == SslPolicyErrors.None)
    {
    return true;
    }
    return false;
    }
    }
    }
    Вызывается просто:
    Код
    SettingMessage.SendToMail("bot@mail.ru", "XoiQviuzEasyPass", "Antlion@mail.ru", "Anonimus", "smtp.mail.ru", 587, 7000);
     
Top