SMTP with MailKit/ MimeKit

Discussion in 'Email' started by Marv Cook, Sep 3, 2019.

  1. Here is a working C# class for Visual Studio:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using MimeKit;
    using SmtpClient = MailKit.Net.Smtp.SmtpClient;

    namespace FalconServer.Library
    {
    public static class EmailProcessor
    {
    public static void SendMail(string to, string subject, string body, DateTime dateTime)
    {
    List<string> toList = new List<string>();
    toList.Add(to);
    SendMail(toList, subject, body, dateTime);
    }

    public static void SendMail(List<string> toList, string subject, string body, DateTime dateTime)
    {
    List<MailboxAddress> toAddresses = toList.Select(address => new MailboxAddress(address)).ToList();
    var ip = "mail.yourDomain.com";
    var port = 25;
    var mail = new MimeMessage();
    mail.From.Add(new MailboxAddress("EmailAccount@yourDomain.com"));
    mail.To.AddRange(toAddresses);
    mail.Subject = subject;
    mail.Body = new TextPart("plain")
    {
    Text = body
    };
    using (var client = new SmtpClient())
    {
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    client.Connect(ip, port, false);
    // Note: only needed if the SMTP server requires authentication
    client.Authenticate("EmailAccount@yourDomain.com", "password");

    client.Send(mail);
    client.Disconnect(true);
    }
    }
    }
    }
     
  2. ComputerMan

    ComputerMan Winhost Staff

    Are you having an issues or are you stating this code works?
     

Share This Page