向用户发送有关信息的邮件

Send mail to user about information

是否有任何库可以在 C# 控制台应用程序中实现,以向用户发送有关某些信息的邮件。就我而言,只要有新内容添加到 ActiveDirectory 域,这就会向用户或管理员发送邮件?

将此添加到您的控制台主要功能,它将完成工作

    class Program
{
    static void Main(string[] args)
    {
            // make sure allow less secure apps on gmail https://myaccount.google.com/lesssecureapps
            SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");

            // set smtp-client properties
            mySmtpClient.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicAuthenticationInfo = new
            System.Net.NetworkCredential("yourusername@gmail.com", "YourGmailPassword");
            mySmtpClient.Credentials = basicAuthenticationInfo;
            mySmtpClient.EnableSsl = true;
            mySmtpClient.Port = 587;

            // add from,to mailaddresses
            MailAddress from = new MailAddress("yourusername@gmail.com", "IAMSender");
            MailAddress to = new MailAddress("receiver@mail.com", "IAMReceiver");
            MailMessage myMail = new System.Net.Mail.MailMessage(from, to);


            // set subject and encoding
            myMail.Subject = "Test message";
            myMail.SubjectEncoding = System.Text.Encoding.UTF8;

            // set body-message and encoding
            myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
            myMail.BodyEncoding = System.Text.Encoding.UTF8;
            // text or html
            myMail.IsBodyHtml = true;

            mySmtpClient.Send(myMail);

        }
}

使用 MailKit 发送带附件的邮件的示例。调整了用于发送消息的 Mailkit 示例并添加了附件代码。

using System;
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;

class Program
{
    public static void Main (string[] args)
    {
        var message = new MimeMessage ();
        message.From.Add (new MailboxAddress ("Sender", "sender@example.com"));
        message.To.Add (new MailboxAddress ("Reciever", "reciever@example.com"));
        message.Subject = "Report";

        var builder = new BodyBuilder ();
        // Set the plain-text version of the message text
        builder.TextBody = @"Hi Reciever,

        Please find the attached report for your view.

        Sender
        ";

        builder.Attachments.Add (@"C:\Users\Sender\Documents\Report.pdf");
        message.Body = builder.ToMessageBody ();

        using (var client = new SmtpClient ()) {
            client.Connect ("smtp.example.com", 587, false);
            client.Authenticate ("sender", "password");
            client.Send (message);
            client.Disconnect (true);
        }
    }
}