ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.net 之邮件发送帮助类 MailKit 的使用

2022-06-27 14:04:22  阅读:223  来源: 互联网

标签:25 MailKit cn smtp 端口 110 net com 邮件


 

1. nuget 安装 MailKit 的引用

源码: https://github.com/jstedfast/MimeKit

 

2.MailKitHelper 的具体代码如下,

public class MailKitHelper
    {
        /// <summary>
        ///发送邮件
        /// </summary>
        /// <param name="toAddressList">接收人</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="attachments">附件</param>
        /// <returns></returns>
        public static void SendMail(List<KeyValuePair<string, string>> toAddressList, string title, string content, List<KeyValuePair<string, byte[]>> attachments = null)
        {
            string server = ConfigurationCache.GetField<string>(ConfigurationCache.Email_SmtpServer);
            int port = ConfigurationCache.GetField<int>(ConfigurationCache.Email_SmtpPort);
            string account = ConfigurationCache.GetField<string>(ConfigurationCache.Email_FromAccount);
            string pwd = ConfigurationCache.GetField<string>(ConfigurationCache.Email_FromAccountPwd);

            var mailMessage = new MimeMessage();
            var fromMailAddress = new MailboxAddress("ScheduleMaster", account);
            mailMessage.From.Add(fromMailAddress);
            var toMailAddress = toAddressList.Select(x => new MailboxAddress(x.Key, x.Value));
            mailMessage.To.AddRange(toMailAddress);

            var bodyBuilder = new BodyBuilder() { HtmlBody = content };
            if (attachments != null)
            {
                foreach (var item in attachments)
                {
                    bodyBuilder.Attachments.Add(item.Key, item.Value);
                }
            }
            mailMessage.Body = bodyBuilder.ToMessageBody();
            mailMessage.Subject = title;
            using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
            {
                smtpClient.Timeout = 10 * 1000;   //设置超时时间
                smtpClient.Connect(server, port, MailKit.Security.SecureSocketOptions.Auto);//连接到远程smtp服务器
                smtpClient.Authenticate(account, pwd);
                smtpClient.Send(mailMessage);//发送邮件
                smtpClient.Disconnect(true);
            }
        }
    }

3.常用的各大邮件服务端口号如下: 

邮箱类型 POP3服务器 SMTP服务器
【QQ邮箱】 pop.qq.com(端口:110) smtp.qq.com(端口:25)
【sina.com】 pop3.sina.com.cn(端口:110) smtp.sina.com.cn(端口:25)
【sinaVIP】 pop3.vip.sina.com (端口:110) smtp.vip.sina.com (端口:25)
【sohu.com】 pop3.sohu.com(端口:110) smtp.sohu.com(端口:25)
【126邮箱】 pop.126.com(端口:110) smtp.126.com(端口:25)
【139邮箱】 POP.139.com(端口:110) SMTP.139.com(端口:25)

【163.com】
pop.163.com(端口:110) smtp.163.com(端口:25)
【QQ企业邮箱】 pop.exmail.qq.com (SSL启用 端口:995) smtp.exmail.qq.com(SSL启用 端口:587/465)
【yahoo.com】 pop.mail.yahoo.com smtp.mail.yahoo.com
【yahoo.com.cn】 pop.mail.yahoo.com.cn(端口:995) smtp.mail.yahoo.com.cn(端口:587)
【HotMail】 pop3.live.com(端口:995) smtp.live.com(端口:587)
【 Gmail】 pop.gmail.com(SSL启用端口:995) smtp.gmail.com(SSL启用 端口:587)
【263.net】 pop3.263.net(端口:110) smtp.263.net(端口:25)
【263.net.cn】 pop.263.net.cn(端口:110) smtp.263.net.cn(端口:25)
【21cn.com】 pop.21cn.com(端口:110 smtp.21cn.com(端口:25)
【Foxmail】 POP.foxmail.com(端口:110) SMTP.foxmail.com(端口:25)
【china.com】 pop.china.com(端口:110) smtp.china.com(端口:25)
【tom.com】 pop.tom.com(端口:110) smtp.tom.com(端口:25)

标签:25,MailKit,cn,smtp,端口,110,net,com,邮件
来源: https://www.cnblogs.com/jerque/p/16415872.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有