.netcore使用MimeKit發送郵件

以163郵箱爲例,藉助MimeKithtml

nuget安裝:MimeKit類庫,源碼地址:https://github.com/jstedfast/MimeKitgit

發送方法以下:github

#region 郵件發送
        /// <summary>
        /// 郵件發送
        /// </summary>
        /// <param name="sendName">發送者名稱</param>
        /// <param name="sendAccountName">發送者帳號</param>
        /// <param name="smtpHost">發送者服務器地址:例如:smtp.163.com</param>
        /// <param name="smtpPort">服務器端口號:例如:25</param>
        /// <param name="authenticatePassword">發送者登陸郵箱帳號的客戶端受權碼</param>
        /// <param name="receiverAccountNameList">接收者帳號</param>
        /// <param name="mailSubject">郵件主題</param>
        /// <param name="sendHtml">文本html(與sendText參數互斥,傳此值則 sendText傳null)</param>
        /// <param name="sendText">純文本(與sendHtml參數互斥,傳此值則 sendHtml傳null)</param>
        /// <param name="accessoryList">郵件的附件</param>
        public static void SendMail(string sendName, string sendAccountName, string smtpHost, int smtpPort, string authenticatePassword, List<string> receiverAccountNameList, string mailSubject, string sendHtml, string sendText, List<MimeKit.MimePart> accessoryList = null)
        {
            var message = new MimeKit.MimeMessage();
            message.From.Add(new MimeKit.MailboxAddress(sendName, sendAccountName));
            var mailboxAddressList = new List<MailboxAddress>();
            receiverAccountNameList.ForEach(f =>
            {

                mailboxAddressList.Add(new MailboxAddress(f));
            });
            message.To.AddRange(mailboxAddressList);

            message.Subject = mailSubject;

            var alternative = new MimeKit.Multipart("alternative");
            if (!string.IsNullOrWhiteSpace(sendText))
            {
                alternative.Add(new MimeKit.TextPart("plain")
                {
                    Text = sendText
                });
            }

            if (!string.IsNullOrWhiteSpace(sendHtml))
            {
                alternative.Add(new MimeKit.TextPart("html")
                {
                    Text = sendHtml
                });
            }
            var multipart = new MimeKit.Multipart("mixed");
            multipart.Add(alternative);
            if (accessoryList != null)
            {
                accessoryList?.ForEach(f =>
                {
                    multipart.Add(f);
                });

            }
            message.Body = multipart;
            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                client.Connect(smtpHost, smtpPort, false);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(sendAccountName, authenticatePassword);
                client.Send(message);
                client.Disconnect(true);
            }
        }

        #endregion

使用方法:服務器

SendMail("發送人名稱", "xxxxxxx@163.com", "smtp.163.com", 25, "xxxxxxxxx受權碼", new List<string> { { "xxxxxx@qq.com" } }, "郵件主題", @" <p>郵件文本</p> ", null, accessoryList: new List<MimePart>()
            {
                {
                    new MimeKit.MimePart("audio","mp4")
                    {
                        ContentObject = new MimeKit.ContentObject(File.OpenRead("C:\\Users\\lyj\\Desktop\\圖片\\下載 (5).mp4"), MimeKit.ContentEncoding.Default),
                        ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                        ContentTransferEncoding = MimeKit.ContentEncoding.Base64,
                        FileName = Path.GetFileName("C:\\Users\\lyj\\Desktop\\圖片\\下載 (5).mp4")
                    }
              },
                {
                    new MimeKit.MimePart("audio","mp4")
                    {
                        ContentObject = new MimeKit.ContentObject(File.OpenRead("C:\\Users\\lyj\\Desktop\\圖片\\下載 (3).mp4"), MimeKit.ContentEncoding.Default),
                        ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                        ContentTransferEncoding = MimeKit.ContentEncoding.Base64,
                        FileName = Path.GetFileName("C:\\Users\\lyj\\Desktop\\圖片\\下載 (3).mp4")
                    }
                },
                {
                    new MimeKit.MimePart("image","jpg")
                    {
                        ContentObject = new MimeKit.ContentObject(File.OpenRead("C:\\Users\\lyj\\Desktop\\圖片\\11timg.jpg"), MimeKit.ContentEncoding.Default),
                        ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                        ContentTransferEncoding = MimeKit.ContentEncoding.Base64,
                        FileName = Path.GetFileName("C:\\Users\\lyj\\Desktop\\圖片\\11timg.jpg")
                    }
                }
        });

 

收錄一些郵件服務地址對應的端口:https://blog.csdn.net/zdqdj1/article/details/48030023spa

相關文章
相關標籤/搜索