1. 程式人生 > >AspNetCore 目前不支援SMTP協議(基於開源元件開發郵件傳送,它們分別是MailKit 和 FluentEmail )

AspNetCore 目前不支援SMTP協議(基於開源元件開發郵件傳送,它們分別是MailKit 和 FluentEmail )

net所有的功能都要重新來一遍,整合眾多類庫,core任重道遠,且發展且努力!!

我們都知道,很多的郵件傳送都是基於這個SMTP協議,但現在的.net core對這方面還不太支援,所以我們選擇這兩個元件MailKit 和 FluentEmail

MailKit與fluentEmail

在 ASP.NET Core 中,可以使用 MailKit 來發送郵件,它支援跨平臺,並且支援 IMAP, POP3, SMTP 等協議。

你可以使用下面的方式安裝:

Install-Package MailKit

直接show程式碼吧!!
using MimeKit;
using System;
using System.IO;

namespace SMTP協議
{
    class Program
    {
        static void Main(string[] args)
        {
            TestSendMailDemo();
        }
        public static void TestSendMailDemo()
        {
            var message = new MimeKit.MimeMessage();
            message.From.Add(new MimeKit.MailboxAddress("zara", "
[email protected]
")); message.To.Add(new MimeKit.MailboxAddress("zaranet", "[email protected]")); message.Subject = "This is a Test Mail"; var plain = new MimeKit.TextPart("plain") { Text = @"不好意思,我在測試程式,Sorry!" }; var html = new MimeKit.TextPart("html") { Text = @"<p>Hey geffzhang<br> <p>不好意思,我在測試程式,Sorry!<br> <p>-- Geffzhang<br>" }; // create an image attachment for the file located at path var path = @"C:\Users\MACHENIKE\Desktop\a.png"; var fs = File.OpenRead(path); var attachment = new MimeKit.MimePart("image", "jpeg") { ContentObject = new MimeKit.ContentObject(fs, MimeKit.ContentEncoding.Default), ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment), ContentTransferEncoding = MimeKit.ContentEncoding.Base64, FileName = Path.GetFileName(path) }; var alternative = new MimeKit.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 MimeKit.Multipart("mixed"); multipart.Add(alternative); multipart.Add(attachment); message.Body = multipart; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.qq.com", 465, true); // 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 var mailFromAccount = "
[email protected]
"; var mailPassword = "xxxx"; client.Authenticate(mailFromAccount, mailPassword); client.Send(message); client.Disconnect(true); } fs.Dispose(); } } }

結果:

BUG::  

1.其中通過mailkit傳送的時候  傳送方必須要開啟自己的POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 一般是在自己的個人中心   2018-11-12  16:11:41

2.一般來說授權碼是在個人中心,而且一定有幫助 裡面有關於他的服務什麼的

 FluentEmail 

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

            }
        }
    }