吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1380|回复: 4
收起左侧

[其他转载] 使用异步子线程发送邮件(各种账号邮件都支持)

  [复制链接]
微笑小生 发表于 2021-12-22 15:43
1、这里帖主分别采用的QQ、腾讯企业邮箱、outlook(包括企业邮箱)

2、废话不多说,直接上代码,这里亲自运行过,直接拷贝使用即可


[C#] 纯文本查看 复制代码
      /// <summary>
        /// 使用其它线程发送邮件,不影响主流程运行
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public static void SendTask(string to, string subject, string body)
        {
            Task.Run(async () =>
            {
                var b = await SendAsync(to, subject, body);
                Log4NetHelper.Info($@"给{to}发送邮件{(b ? "成功" : "失败")}");
            });
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static async Task<bool> SendAsync(string to, string subject, string body)
        {
            return await SendAsync(to, "", "", subject, body, "");
        }

        /// <summary>
        /// 请求远程接口发送邮件
        /// </summary>
        /// <param name="toes">收件人,多个收件人以逗号(,)隔开</param>
        /// <param name="ces">抄送人,多个抄送人以逗号(,)隔开</param>
        /// <param name="bes">密送人,多个密送人以逗号(,)隔开</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="displayName">发件人邮箱别名</param>
        /// <param name="attachments">附件</param>
        /// <returns></returns>
        public static Task<bool> SendAsync(string toes, string ces, string bes, string subject, string body,
            string displayName = "", List<Attach> attachments = null)
        {
            return EmailServer.SendAsync(toes, ces, bes, subject, body, displayName, attachments);
        }
    }

        public async Task<bool> SendAsync(string toes, string ces, string bes, string subject, string body, string displayName = "",
            List<Attach> attachments = null)
        {
            var message = attachments != null && attachments.Any()
                ? CreateMessageIncludeAttach(toes, ces, bes, subject, body, displayName, attachments)
                : CreateMessage(toes, ces, bes, subject, body, displayName);
            return await SendAsync(message);
        }
  /// <summary>
        /// 发送邮件请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public static async Task<bool> SendAsync(HttpRequestMessage requestMessage)
        {
            ServicePointManager.SecurityProtocol =
                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            try
            {
                var responseMessage = await Client.SendAsync(requestMessage);
                if (responseMessage.IsSuccessStatusCode)
                {
                    var res = await responseMessage.Content.ReadAsStringAsync();
                    Log4NetHelper.Info($"邮件请求响应:{res}");
                    return res.Contains("true");
                }

                Log4NetHelper.Info($"邮件发送失败,请求响应码{responseMessage.StatusCode}");
                return false;
            }
            catch (Exception e)
            {
                Log4NetHelper.Error($"邮件发送发生异常:{e.Message}", e);
                return false;
            }
        }







3、直接调用即可,需要分批发送邮件这个最为方便了,采用的是异步进程,不会阻塞程序

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ipc2008 发表于 2021-12-22 16:56
EmailServer和Log4NetHelper这两个类提供一下?
lvmins 发表于 2021-12-22 17:49
 楼主| 微笑小生 发表于 2021-12-22 17:59
ipc2008 发表于 2021-12-22 16:56
EmailServer和Log4NetHelper这两个类提供一下?

[C#] 纯文本查看 复制代码
private static readonly IEmailServer EmailServer;

        static NewEmailHelper()
        {
            //通过不通的实例化选择不同的实现方式
            // EmailServer = new ApiEmailServer();
            EmailServer = new MailKitServer();
        }

        /// <summary>
        /// 使用其它线程发送邮件,不影响主流程运行
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public static void SendTask(string to, string subject, string body)
        {
            Task.Run(async () =>
            {
                var b = await SendAsync(to, subject, body);
                Log4NetHelper.Info($@"给{to}发送邮件{(b ? "成功" : "失败")}");
            });
        }

        /// <summary>
        /// 使用其它线程发送邮件,不影响主流程运行
        /// </summary>
        /// <param name="toes"></param>
        /// <param name="ces"></param>
        /// <param name="bes"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="displayName"></param>
        /// <param name="attachments"></param>
        public static void SendTask(string toes, string ces, string bes, string subject, string body,
            string displayName = "", List<Attach> attachments = null)
        {
            Task.Run(async () =>
            {
                var b = await SendAsync(toes, ces, bes, subject, body, displayName, attachments);
                Log4NetHelper.Info($@"给{toes}发送邮件{(b ? "成功" : "失败")}");
            });
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static async Task<bool> SendAsync(string to, string subject, string body)
        {
            return await SendAsync(to, "", "", subject, body, "");
        }

        /// <summary>
        /// 请求远程接口发送邮件
        /// </summary>
        /// <param name="toes">收件人,多个收件人以逗号(,)隔开</param>
        /// <param name="ces">抄送人,多个抄送人以逗号(,)隔开</param>
        /// <param name="bes">密送人,多个密送人以逗号(,)隔开</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="displayName">发件人邮箱别名</param>
        /// <param name="attachments">附件</param>
        /// <returns></returns>
        public static Task<bool> SendAsync(string toes, string ces, string bes, string subject, string body,
            string displayName = "", List<Attach> attachments = null)
        {
            return EmailServer.SendAsync(toes, ces, bes, subject, body, displayName, attachments);
        }





log4net可以生产一个公共类,这里我只是记录错误日志,你可以采用其他的也行的
 楼主| 微笑小生 发表于 2021-12-22 18:00
lvmins 发表于 2021-12-22 17:49
EmailServer 没有啊

已经发到评论区了,多谢支持
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 11:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表