Send mail to user about information

陌路散爱 提交于 2021-01-29 15:52:42

问题


Is there any libary which can be implemented in C# Console App to send mail to user about some information. In my case, this will be send mail to user or admin whenever new has been added to ActiveDirectory domain ?


回答1:


add this to your console main function and it will do the work

    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);

        }
}



回答2:


A sample for sending the mail with attachment using the MailKit. Tweaked the Mailkit sample for sending the message and added the attachment code.

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);
        }
    }
}


来源:https://stackoverflow.com/questions/61586332/send-mail-to-user-about-information

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!