c# MailKit3.4.3 发送邮件、附件

    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 (!attachFileList.IsNullOrEmpty())
            {
                foreach (var attachFile in attachFileList)
                {
                    var fileInfo = new FileInfo(attachFile.FilePath);
                    using (var fileStream = File.OpenRead(attachFile.FilePath))
                    {
                        var memoryStream = new MemoryStream();
                        fileStream.CopyTo(memoryStream);
                        memoryStream.Position = 0; // 重置位置

                        MimePart attachment = new MimePart()
                        {
                            Content = new MimeContent(memoryStream, ContentEncoding.Default),
                            ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                            ContentTransferEncoding = ContentEncoding.Base64,
                            FileName = attachFile.FileName
                        };
                        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);
        }
    }
}
posted @ 2026-05-22 10:16  Hey,Coder!  阅读(13)  评论(0)    收藏  举报