惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
D
DataBreaches.Net
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
S
Secure Thoughts
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
V2EX - 技术
V2EX - 技术
腾讯CDC
GbyAI
GbyAI
G
Google Developers Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Martin Fowler
Martin Fowler
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI

博客园 - Hey,Coder!

Elasticsearch ILM 与 Data Stream 概念及实例说明文档 portainer httphelper封装 Ocelot + Consul + SignalR 粘性会话 正交软件架构 docker postgresql17 主从复制 rabbitmq总结与c#示例 docker rabbitmq quartz dashboard docker consul registrator 服务自动注册consul 微服务动态扩容 ubuntu24.04 安装docker RAG 检索增强生成 软考 - 架构设计师 知识点总结 nginx 反向代理postgresql c# 信号量 elsa 3.5 中间件记录最后执行的节点数据 xp密钥 c# System.Text.Json 反序列化Dictionary<string,object>时未转换基础类型的处理方法 docker配置代理 openclaw 接入 LMStudio的模型服务 wpf canvas 移动 缩放 windows平台openclaw搭建 wpf scrollerview触摸滚动 c# 动态切换sqlsugar连接字符串 c# Elastic.Clients.Elasticsearch 动态查询 c# es 封装Elastic.Clients.Elasticsearch c# scrollerview滚动到指定元素位置 WPF 重写Expander 基于elsa工作流封装一套变量、组件的体系 常用活动重写 DynamicExpresso 轻量级动态表达式库 c# elsa 3.5.2 程序化工作流常用功能及自定义中间件 leaflet docker 连接老版本的sqlserver ssl错误 c# quartz 动态创建任务 控制任务运行和停止 依赖注入 cefsharp 模拟点击 批量重置数据库的数据 docker 复制远程镜像本地并创建容器 c# newtonsoft dynamic
c# MailKit3.4.3 发送邮件、附件
Hey,Coder! · 2026-05-22 · via 博客园 - Hey,Coder!
    public class MailConfig 
    {
        /// <summary>
        /// 超时时长 - 毫秒
        /// </summary>
        public int Timeout { get; set; } = 50000;
        /// <summary>
        /// 发件邮箱
        /// </summary>
        public string MailFrom { get; set; }
        /// <summary>
        /// 邮箱密码
        /// </summary>
        public string MailPwd { get; set; }
        /// <summary>
        /// 发件服务器
        /// </summary>
        public string MailHost { get; set; }

        /// <summary>
        /// 端口
        /// </summary>
        public int MailPort { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public SecureSocketOptions SecurityType { get; set; } = SecureSocketOptions.Auto;
    }

public static class MailHelper
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="config">服务器配置</param>
    /// <param name="subject">主题</param>
    /// <param name="content">正文</param>
    /// <param name="mailTo">接收邮箱</param>
    /// <returns></returns>
    public static void SendMail(MailConfig config, string subject, string content, List<string> mailTo, int timeout = 10000, List<string> fileList = null)
    {
        MimeMessage message = new MimeMessage();
        message.From.Add(new MailboxAddress(config.MailFrom, config.MailFrom));

        if (mailTo.IsNullOrEmpty())
        {
            throw new Exception("maill to null user");
        }

        foreach (string item in mailTo)
        {
            message.To.Add(new MailboxAddress(item, item));
        }

        message.Subject = string.Format(subject);
        using (MailKit.Net.Smtp.SmtpClient client = new MailKit.Net.Smtp.SmtpClient())
        {
            client.Timeout = timeout;
            client.Connect(config.MailHost, config.MailPort, config.SecurityType);
            Multipart multipart = new Multipart("mixed");

            if (!fileList.IsNullOrEmpty())
            {
                foreach (string filePath in fileList)
                {
                    var fileInfo = new FileInfo(filePath);
                    MimePart attachment = new MimePart()
                    {
                        Content = new MimeContent(File.OpenRead(fileInfo.FullName), ContentEncoding.Default),
                        ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                        ContentTransferEncoding = ContentEncoding.Base64,
                        FileName = fileInfo.Name
                    };
                    multipart.Add(attachment);
                }
            }

            //var textBody = new TextPart("plain")
            var textBody = new TextPart("html")
            {
                Text = content
            };

            multipart.Add(textBody);
            message.Body = multipart;

            client.Authenticate(config.MailFrom, config.MailPwd);
            client.Send(message);
            client.Disconnect(true);
        }
    }
}