博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Email
阅读量:5861 次
发布时间:2019-06-19

本文共 6780 字,大约阅读时间需要 22 分钟。

https://github.com/jstedfast/MailKit

https://github.com/jstedfast/MimeKit

https://www.cnblogs.com/pengze0902/p/6562447.html

public class EmailHelp    {        ///         /// Smtp服务器地址        ///         private static readonly string SmtpServer = ConfigurationManager.AppSettings["SmtpServer"];        ///         /// Pop服务器地址        ///         private static readonly string PopServer = ConfigurationManager.AppSettings["PopServer"];        ///         /// Imap服务器地址        ///         private static readonly string ImapServer = ConfigurationManager.AppSettings["ImapServer"];        ///         /// SMTP端口        ///         private static readonly int SmtpPort = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);        ///         /// POP端口        ///         private static readonly int PopPort = int.Parse(ConfigurationManager.AppSettings["PopPort"]);        ///         /// IMAP端口        ///         private static readonly int ImapPort = int.Parse(ConfigurationManager.AppSettings["ImapPort"]);        ///         /// 邮件发送        ///         /// 发送邮箱账号        /// 发送邮箱密码        /// 邮件        public static void SendEmali(string mailFromAccount, string mailPassword, MimeMessage message)        {            using (var client = new MailKit.Net.Smtp.SmtpClient())            {                client.Connect(SmtpServer, SmtpPort, 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);            }        }        ///         /// 创建文本消息        ///         /// 发件地址        /// 收件地址        /// 标题        /// 内容        /// 是否将POST上传文件加为附件        /// 
public static MimeMessage CreateTextMessage(MailboxAddress fromAddress, IList
toAddressList , string title, string content, bool IsPostFiles = false) { var message = new MimeMessage(); message.From.Add(fromAddress); message.To.AddRange(toAddressList); message.Subject = title; //设置消息的主题 var html = new TextPart("html") { Text = content, }; var alternative = new Multipart("alternative"); alternative.Add(html); var multipart = new Multipart("mixed"); multipart.Add(alternative); if (IsPostFiles) { IList
multiPartList = GetMimePartList(); foreach (var item in multiPartList) { multipart.Add(item); } } message.Body = multipart; return message; } ///
/// 收邮件 /// ///
发送邮箱账号 ///
发送邮箱密码 ///
查询条件 ///
文件夹名称 ///
public static IList
ReceiveEmail(string mailFromAccount, string mailPassword, string folderName, SearchQuery searchQuery = null) { //打开收件箱 var folder = OpenFolder(mailFromAccount, mailPassword, folderName); //IList
orderByList = new List
{ OrderBy.Date }; //查询所有的邮件 var uidss = folder.Search(searchQuery); IList
msgList = new List
(); if (uidss.Count > 0)//判断是否查询到邮件 { //获取邮件头 msgList = folder.Fetch(uidss, MessageSummaryItems.UniqueId | MessageSummaryItems.Full); } folder.Close(); return msgList; } ///
/// 根据唯一号查询信件 /// ///
邮箱账号 ///
邮箱密码 ///
唯一号 ///
文件夹名称 ///
public static MimeMessage GetEmailByUniqueId(string mailFromAccount, string mailPassword, uint id, string folderName) { //打开收件箱 var folder = OpenFolder(mailFromAccount, mailPassword, folderName); UniqueId emailUniqueId = new UniqueId(id); MimeMessage message = folder.GetMessage(emailUniqueId); /*将邮件设为已读*/ MessageFlags flags = MessageFlags.Seen; folder.SetFlags(emailUniqueId, flags, true); return message; } ///
/// 读取上传的文件 /// ///
public static IList
GetMimePartList() { IList
mimePartList = new List
(); var current = HttpContext.Current; if (current != null) { HttpRequest request = current.Request; HttpFileCollection files = request.Files; int filesCount = files.Count; for (int i = 0; i < filesCount; i++) { HttpPostedFile item = files[i]; MimePart attachment = new MimePart(item.ContentType) { ContentObject = new ContentObject(item.InputStream, ContentEncoding.Default), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = item.FileName }; mimePartList.Add(attachment); } } return mimePartList; } ///
/// 打开邮箱文件夹 /// ///
邮箱账号 ///
邮箱密码 ///
文件夹名称(INBOX:收件箱名称) ///
public static IMailFolder OpenFolder(string mailFromAccount, string mailPassword, string folderName) { ImapClient client = new ImapClient(); client.Connect(ImapServer, ImapPort); client.Authenticate(mailFromAccount, mailPassword); //获取所有文件夹 //List
mailFolderList = client.GetFolders(client.PersonalNamespaces[0]).ToList(); var folder = client.GetFolder(folderName); //打开文件夹并设置为读的方式 folder.Open(MailKit.FolderAccess.ReadWrite); return folder; } ///
/// 下载邮件附件 /// ///
public static void DownFile(MimePart mimePart) { HttpContext context = HttpContext.Current; // 设置编码和附件格式 context.Response.ContentType = mimePart.ContentType.ToString(); //context.Response.ContentEncoding = Encoding.UTF8; context.Response.Charset = ""; string fileName = HttpUtility.UrlEncode(mimePart.FileName, Encoding.UTF8); context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); using (MemoryStream ms = new MemoryStream()) { mimePart.ContentObject.DecodeTo(ms); ms.Flush(); ms.Position = 0; context.Response.BinaryWrite(ms.GetBuffer()); context.Response.End(); } } }
View Code

http://www.mimekit.net/

MailKit正式替换了.NET的SmtpClient

SmtpClient的文档现已改成:“废弃(“SmtpClient及其相关类型设计很差,我们强烈建议使用和替代。”)”。这是Microsoft有史以来第二次将一个.NET类正式标为被开源软件库替代。

和的创建者是Jeffrey Stedfast,InfoQ曾在。在当时,它们已被认为是.NET上最全面的MIME和电子邮件库。

是被Microsoft接受的首个重要开源库。JSON.NET已在ASP.NET Web API中广泛使用,并被正式推荐为Web API使用的序列化类,通常可替代类。但是不同于SmtpClient的是,没有任何一个序列化类因此被标记为废弃。

client.Inbox.Open (FolderAccess.ReadOnly);var uids = client.Inbox.Search (SearchQuery.SubjectContains ("HELLO_"));if (uids.Count > 0) {    var summaries = client.Inbox.Fetch (uids, MessageSummaryItems.Envelope);    foreach (var summary in summaries) {        if (summary.Envelope.Subject.StartsWith ("HELLO_"))            return summary.Envelope.Subject;    }}
View Code

 

你可能感兴趣的文章
commonservice-config配置服务搭建
查看>>
连接池的意义及阿里Druid
查看>>
Python递归函数与匿名函数
查看>>
loadrunner安装运行一步一步来(多图)
查看>>
监控工具htop的安装及使用
查看>>
Nodejs使用图灵机器人获取笑话
查看>>
Spring 任务调度 简单的,使用Schedule
查看>>
SQL 2005删除作业计划出错(DELETE语句与 REFERENCE约束"FK_subplan_job_id"冲突。)的解决...
查看>>
【Touch&input 】支持多个游戏控制器(18)
查看>>
SQL语句学习
查看>>
进老男孩的自我介绍和决心书
查看>>
线上Linux服务器运维安全策略经验分享
查看>>
Android一些问题的解决方案
查看>>
Luogu P4707 重返现世
查看>>
目标与绩效管理实战专家胡立
查看>>
2014手机分析图
查看>>
一元多项式相加
查看>>
commandLink/commandButton/ajax backing bean action/listener method not invoked (转)
查看>>
(一) pyhon 基础语法(数值 字符串 元组 列表 字典)
查看>>
HDOJ 1003:求一串数字中和最大的连续子串
查看>>