C#发送邮件

基础调用类:

cs 复制代码
public class EmailHelper
    {
        /// <summary>
        /// 发件人名称
        /// </summary>
        public string MailName { get; set; }

        /// <summary>
        /// 收件人
        /// </summary>
        public string MailTo { get; set; }

        /// <summary>
        /// 密送
        /// </summary>
        public string MailBCC { get; set; }

        /// <summary>
        /// 抄送
        /// </summary>
        public string MailCC { get; set; }


        /// <summary>
        /// 主题
        /// </summary>
        public string MailSubject { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string MailHTMLBody { get; set; }

        /// <summary>
        /// 附加
        /// </summary>
        public string MailAttachments { get; set; }
        public string MailAttachments2 { get; set; }//第二个附件
        public bool Send(ref string message)
        {
            try
            {

                //發送郵件            
                MailMessage myEmail = new MailMessage();

                myEmail.From = new MailAddress(string.Format("{0}@xxxxx.com.cn", MailName));//mail from
                                                                                             //myEmail.To.Add(new MailAddress(MailTo));
                string[] MailToAll = MailTo.Split(';');
                for (int i = 0; i < MailToAll.Length; i++)
                {
                    myEmail.To.Add(new MailAddress(MailToAll[i]));
                }

                // 設定密送 2022-10-21 tom
                if (!string.IsNullOrEmpty(MailBCC))
                {
                    string[] MailBCCAll = MailBCC.Split(';');
                    for (int i = 0; i < MailBCCAll.Length; i++)
                    {
                        myEmail.Bcc.Add(new MailAddress(MailBCCAll[i]));
                    }
                }

                myEmail.Subject = MailSubject;//郵件主題
                if (MailAttachments != null)
                {
                    myEmail.Attachments.Add(new Attachment(MailAttachments));
                }
                //myEmail.Attachments.Add(new Attachment(MailAttachments));
                if (MailAttachments2 != null)
                {
                    myEmail.Attachments.Add(new Attachment(MailAttachments2));
                }
                AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(MailHTMLBody, null, "text/html");
                myEmail.AlternateViews.Add(htmlBody);

                myEmail.BodyEncoding = Encoding.UTF8; //編碼格式
                myEmail.Priority = MailPriority.Normal; //重要性 

                SmtpClient smtp = new SmtpClient();
                smtp.UseDefaultCredentials = true;

                smtp.Port = 25;
                smtp.Host = "10.3.1.1";
                smtp.EnableSsl = false;
                smtp.Send(myEmail);
                message = "发送成功";
                return true;
            }
            catch (System.Exception ex)
            {
                message = ex.ToString();
                return false;
            }
        }
    }

调用类:

cs 复制代码
 //郵件發送
        public bool emails(string MailFrom, string MailTo, string MailSubject, string Content, string FilePath)
        {
            try
            {
                EmailHelper EMAIL = new EmailHelper();
                messageString = "生成成功";
                EMAIL.MailName = MailFrom + System.DateTime.Now.ToString("yyyyMMdd");
                EMAIL.MailTo = MailTo;
                //EMAIL.MailTo = mEmailResult;
                //EMAIL.MailCC = "Kay@xxxxx.com.cn;";
                //EMAIL.MailBCC = "Kay@xxxxx.com.cn";
                EMAIL.MailSubject = MailSubject + DateTime.Now.ToString("yyyy-MM-dd");
                EMAIL.MailHTMLBody = "<table>";
                EMAIL.MailHTMLBody = "<table><tr><td>Dear Sirs:</td></tr><tr><td>&nbsp;</td></tr><tr><td style='color:red'>" + Content + "</td></tr>";
                EMAIL.MailHTMLBody += "<tr><td><span style='background:aqua;mso-highlight:aqua'></span></td></tr></table>";
                //EMAIL.MailAttachments = fileName;
                EMAIL.MailAttachments = FilePath;
                resultBool = EMAIL.Send(ref messageString);
                return true;
                if (!resultBool)
                {
                    messageString = "郵件發送失敗!" + messageString;
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

实例:

cs 复制代码
            string filePath = "产量.xlsx"; // 替换为你的文件路径
            MailFrom = "Information";//发件人
            MailTo = "Kay@xxxxx.com.cn;Dai@xxxxx.com.cn";//收件人
            MailSubject = "测试数据";//主题
            Content ="" +Line_N + "-每小时产量.xlsx 附件,请查收!";//附件
            if (emails(MailFrom, MailTo, MailSubject, Content, filePath))
            {
                MessageBox.Show("发送成功");
            }
            else
            {
                MessageBox.Show("发送失败");
            }

以上代码可以在项目中添加一个按钮进行测试。

结语:本文主要分享如何用C#代码发送信息给邮件,亲测可用。

相关推荐
唯情于酒4 分钟前
net core web api 使用log4net
c#·.net core
SunnyDays101129 分钟前
C# 实战:快速查找并高亮 Word 文档中的文字(普通查找 + 正则表达式)
开发语言·c#
人工智能AI技术33 分钟前
Qwen3.5-Plus登顶|C#集成通义千问,高并发服务实战优化
人工智能·c#
SunnyDays10112 小时前
如何使用 C# 在 Word 文档中插入超链接 (含文本与图片链接)
开发语言·c#
一念春风2 小时前
证件照制作工具(WPF C#)
c#·wpf
似水明俊德10 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
阿蒙Amon12 小时前
C#常用类库-详解SerialPort
开发语言·c#
似水明俊德13 小时前
02-C#.Net-反射-学习笔记
开发语言·笔记·学习·c#·.net
.NET修仙日记19 小时前
Acme.ReturnOh:让.NET API返回值处理更优雅,统一响应格式一步到位
c#·.net·webapi
阿蒙Amon20 小时前
C#常用类库-详解YamlDotNet
开发语言·c#