文章出處

MimeKit 給.NET 社區帶來了一流的 MIME 解析器,它能處理復雜的各種Mime, 性能好。而且開箱即用支持 S/MIME 和 PGP。MimeKit 和 MailKit 支持最新的國際化的電子郵件標準,是.NET 中為一個支持完整支持這些標準電子郵件庫,最近正式發布了1.0版本。如果你想做所有與的電子郵件相關的事情,看看 MimeKit 和 MailKit。我保證你不會失望,它支持.NET/Mono的所有平臺,包括移動電話、平板等。

使用方法很簡單和.NET的SmtpClient 差不多,不過比它優雅多了,下面show個代碼:

using MimeKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using System.IO;

namespace NetSmtpClient
{
    class Program
    {
        const string mailFrom = "xxxx@hotmail.com";
        const string mailTo = "xxxx@qq.com";
        const string mailFromAccount = "xxxx@hotmail.com";
        const string mailPassword = "xxxx";
        const string path = @"E:\GitHub\TestMailClient\NetSmtpClient\.NETFoundation.png";
        static void Main(string[] args)
        {
            TestSmtpClient();

            TestMailKit();

        }

        private static void TestMailKit()
        {
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("geffzhang", mailFrom));
            message.To.Add(new MailboxAddress("geffzhang", mailTo));
            message.Subject = string.Format("C#自動發送郵件測試 From geffzhang TO {0}", mailTo);

            var plain = new TextPart("plain")
            {
                Text = @"不好意思,我在測試程序,剛才把QQ號寫錯了,Sorry!"
            };
            var html = new TextPart("html")
            {
                Text = @"<p>Hey geffzhang<br>
<p>不好意思,我在測試程序,剛才把QQ號寫錯了,Sorry!<br>
<p>-- Geffzhang<br>"
            };
            // create an image attachment for the file located at path
            var attachment = new MimePart("image", "png")
            {
                ContentObject = new ContentObject(File.OpenRead(path), ContentEncoding.Default),
                ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = Path.GetFileName(path)
            };

            var alternative = new Multipart("alternative");
            alternative.Add(plain);
            alternative.Add(html);

            // now create the multipart/mixed container to hold the message text and the
            // image attachment
            var multipart = new Multipart("mixed");
            multipart.Add(alternative);
            multipart.Add(attachment);
            message.Body = multipart;

            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                client.Connect("smtp.live.com", 587, false);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate(mailFromAccount, mailPassword);

                client.Send(message);
                client.Disconnect(true);
            }
        }

        private static void TestSmtpClient()
        {
            MailMessage mymail = new MailMessage();
            mymail.From = new System.Net.Mail.MailAddress(mailFrom);
            mymail.To.Add(mailTo);
            mymail.Subject = string.Format("C#自動發送郵件測試 From geffzhang TO {0}",mailTo);
            mymail.Body = @"<p>Hey geffzhang<br><p>不好意思,我在測試程序,剛才把QQ號寫錯了,Sorry!<br><p>-- Geffzhang<br>";
            mymail.IsBodyHtml = true;
            mymail.Attachments.Add(new Attachment(path));

            System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
            smtpclient.Port = 587;
            smtpclient.UseDefaultCredentials = false;
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Host = "smtp.live.com";
            smtpclient.EnableSsl = true;
            smtpclient.Credentials = new System.Net.NetworkCredential(mailFromAccount, mailPassword);
            try
            {
                smtpclient.Send(mymail);
                Console.WriteLine("發送成功");


            }
            catch (Exception ex)
            {
                Console.WriteLine("發送郵件失敗.請檢查是否為qq郵箱,并且沒有被防護軟件攔截" + ex);

            }
        }
    }
}
上面代碼是smtp發送代碼,這個庫還支持POP3, 和 IMAP 等。

文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()