C#发送邮件

本文介绍了一个使用 C# 实现的邮件发送类,支持抄送、密送及附件功能,并提供了配置文件读取 SMTP 设置的方法。此外,还提供了一个具体的发送邮件实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

邮件发送类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Web;
using System.IO;
using System.Net;
using System.Net.Mail;

namespace Maticsoft.Common
{
    /// <summary>
    ///  邮件发送类
    /// </summary>
    public class MailSender
    {

        public static void Send(string tomail, string bccmail, string subject, string body, params string[] files)
        {
            Send(SmtpConfig.Create().SmtpSetting.Sender, tomail, bccmail, subject, body, true, Encoding.Default, true, files);
        }        


        public static void Send(string frommail, string tomail,string bccmail, string subject,
                        string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
        {            
            Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.UserName, SmtpConfig.Create().SmtpSetting.Password, frommail,
                tomail,"", bccmail, subject, body, isBodyHtml, encoding, isAuthentication,false, files);
        }


        //直接调用该方法发送邮件,可抄送,密送,逗号分隔多人
        public static void Send(string server,string username,string password, string frommail, string tomail, string ccmail,string bccmail, string subject,
                        string body, bool isBodyHtml, Encoding encoding, bool isAuthentication,bool isSsl, params string[] files)
        {


            SmtpClient smtpClient = new SmtpClient(server);
            //MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
            //MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
            MailMessage message = new MailMessage(frommail, tomail);

            if (bccmail.Length > 1)
            {
                string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(bccmail);
                foreach (string m in maillist)
                {
                    if (m.Trim() != "")
                    {
                        MailAddress bcc = new MailAddress(m.Trim());
                        message.Bcc.Add(bcc);
                    }
                }
            }
            if (ccmail.Length > 1)
            {
                string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(ccmail);
                foreach (string m in maillist)
                {
                    if (m.Trim() != "")
                    {
                        MailAddress cc = new MailAddress(m.Trim());
                        message.CC.Add(cc);
                    }
                }
            }            
            message.IsBodyHtml = isBodyHtml;
            message.SubjectEncoding = encoding;
            message.BodyEncoding = encoding;

            message.Subject = subject;
            message.Body = body;

            message.Attachments.Clear();
            if (files != null && files.Length != 0)
            {
                for (int i = 0; i < files.Length; ++i)
                {
                    Attachment attach = new Attachment(files[i]);
                    message.Attachments.Add(attach);
                }
            }

            if (isAuthentication == true)
            {
                smtpClient.Credentials = new NetworkCredential(username, password);
                smtpClient.EnableSsl =isSsl;
            }
            smtpClient.Send(message);
            message.Attachments.Dispose();

        }

        /// <summary>
        /// 重载发送邮件的方法(增加邮件的回复地址)
        /// </summary>
        /// <param name="server"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="frommail"></param>
        /// <param name="tomail"></param>
        /// <param name="ccmail"></param>
        /// <param name="bccmail"></param>
        /// <param name="replymail"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isBodyHtml"></param>
        /// <param name="encoding"></param>
        /// <param name="isAuthentication"></param>
        /// <param name="isSsl"></param>
        /// <param name="files"></param>
        public static void Send(string server,string username,string password, string frommail, string tomail, string ccmail,string bccmail, string replymail,string subject,
                        string body, bool isBodyHtml, Encoding encoding, bool isAuthentication,bool isSsl, params string[] files)
        {


            SmtpClient smtpClient = new SmtpClient(server);
            //MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
            //MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
            MailMessage message = new MailMessage(frommail, tomail);

            if (bccmail.Length > 1)
            {
                string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(bccmail);
                foreach (string m in maillist)
                {
                    if (m.Trim() != "")
                    {
                        MailAddress bcc = new MailAddress(m.Trim());
                        message.Bcc.Add(bcc);
                    }
                }
            }
            if (ccmail.Length > 1)
            {
                string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(ccmail);
                foreach (string m in maillist)
                {
                    if (m.Trim() != "")
                    {
                        MailAddress cc = new MailAddress(m.Trim());
                        message.CC.Add(cc);
                    }
                }
            }
            if (replymail.Length > 1)
            {
                string[] maillist = Maticsoft.Common.StringPlus.GetStrArray(replymail);
                foreach (string m in maillist)
                {
                    if (m.Trim() != "")
                    {
                        MailAddress cc = new MailAddress(m.Trim());
                        message.ReplyToList.Add(cc);
                    }
                }
            }
            message.IsBodyHtml = isBodyHtml;
            message.SubjectEncoding = encoding;
            message.BodyEncoding = encoding;

            message.Subject = subject;
            message.Body = body;

            message.Attachments.Clear();
            if (files != null && files.Length != 0)
            {
                for (int i = 0; i < files.Length; ++i)
                {
                    Attachment attach = new Attachment(files[i]);
                    message.Attachments.Add(attach);
                }
            }

            if (isAuthentication == true)
            {
                smtpClient.Credentials = new NetworkCredential(username, password);
                smtpClient.EnableSsl = true;
            }
            smtpClient.Send(message);
            message.Attachments.Dispose();

        }

        public static void Send(string recipient, string subject, string body)
        {
            Send( SmtpConfig.Create().SmtpSetting.Sender, recipient,"", subject, body, true, Encoding.Default, true, null);
        }

        public static void Send(string Recipient, string Sender, string Subject, string Body)
        {
            Send(Sender, Recipient, "",Subject, Body, true, Encoding.UTF8, true, null);
        }
                
    }

    public class SmtpSetting
    {
        private string _server;

        public string Server
        {
            get { return _server; }
            set { _server = value; }
        }
        private bool _authentication;

        public bool Authentication
        {
            get { return _authentication; }
            set { _authentication = value; }
        }
        private string _username;

        public string UserName
        {
            get { return _username; }
            set { _username = value; }
        }
        private string _sender;

        public string Sender
        {
            get { return _sender; }
            set { _sender = value; }
        }
        private string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
    }

    public class SmtpConfig
    {
        private static SmtpConfig _smtpConfig;
        private string ConfigFile
        {
            get
            {
                string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
                if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
                {
                    configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
                }
                else
                {
                    if (!Path.IsPathRooted(configPath))
                        configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
                    else
                        configPath = Path.Combine(configPath, "SmtpSetting.config");
                }
                return configPath;
            }
        }
        public SmtpSetting SmtpSetting
        {
            get
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(this.ConfigFile);
                SmtpSetting smtpSetting = new SmtpSetting();
                smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
                smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
                smtpSetting.UserName = doc.DocumentElement.SelectSingleNode("User").InnerText;
                smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
                smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;

                return smtpSetting;
            }
        }
        private SmtpConfig()
        {

        }
        public static SmtpConfig Create()
        {
            if (_smtpConfig == null)
            {
                _smtpConfig = new SmtpConfig();
            }
            return _smtpConfig;
        }
    }
}
View Code

分割多个收件人:

#region 获取用逗号分割后的字符数组
        /// <summary>
        /// 获取用逗号分割后字符数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string[] GetStrArray(string str)
        {
            return str.Split(new Char[] { ',' });
        }
        #endregion
View Code

调用发邮件的方法:(示例是企业qq邮箱)

 public void SendEmail(string message)
        {
            var ToEmail = System.Configuration.ConfigurationManager.AppSettings["toEmail"].ToString();
            var csEmail = System.Configuration.ConfigurationManager.AppSettings["csEmail"].ToString();
            var msEmail = System.Configuration.ConfigurationManager.AppSettings["msEmail"].ToString();
            Maticsoft.Common.MailSender.Send("smtp.exmail.qq.com", "发件人", "发件密码", "发件人", ToEmail, csEmail, msEmail, "邮件主题", message, false, Encoding.UTF8, true, true, null);
        }

 

转载于:https://www.cnblogs.com/SmilePastaLi/p/7159520.html

内容概要:本文深入解析了扣子COZE AI编程及其详细应用代码案例,旨在帮助读者理解新一代低门槛智能体开发范式。文章从五个维度展开:关键概念、核心技巧、典型应用场景、详细代码案例分析以及未来发展趋势。首先介绍了扣子COZE的核心概念,如Bot、Workflow、Plugin、Memory和Knowledge。接着分享了意图识别、函数调用链、动态Prompt、渐进式发布及监控可观测等核心技巧。然后列举了企业内部智能客服、电商导购助手、教育领域AI助教和金融行业合规质检等应用场景。最后,通过构建“会议纪要智能助手”的详细代码案例,展示了从需求描述、技术方案、Workflow节点拆解到调试与上线的全过程,并展望了多智能体协作、本地私有部署、Agent2Agent协议、边缘计算插件和实时RAG等未来发展方向。; 适合人群:对AI编程感兴趣的开发者,尤其是希望快速落地AI产品的技术人员。; 使用场景及目标:①学习如何使用扣子COZE构建生产级智能体;②掌握智能体实例、自动化流程、扩展能力和知识库的使用方法;③通过实际案例理解如何实现会议纪要智能助手的功能,包括触发器设置、下载节点、LLM节点Prompt设计、Code节点处理和邮件节点配置。; 阅读建议:本文不仅提供了理论知识,还包含了详细的代码案例,建议读者结合实际业务需求进行实践,逐步掌握扣子COZE的各项功能,并关注其未来的发展趋势。
标题基于Django+Vue的双相情感障碍交流平台研究AI更换标题第1章引言介绍双相情感障碍交流平台的背景、意义、研究现状以及论文的研究方法和创新点。1.1研究背景与意义阐述双相情感障碍交流平台的重要性和实际需求。1.2国内外研究现状分析国内外在双相情感障碍交流平台方面的研究进展。1.3研究方法与创新点介绍论文采用的研究方法和平台的创新之处。第2章相关理论技术介绍Django、Vue以及双相情感障碍相关理论知识。2.1Django框架概述简述Django框架的特点、优势及其在Web开发中的应用。2.2Vue框架概述阐述Vue框架的核心思想、特点及其在前端开发中的应用。2.3双相情感障碍理论知识介绍双相情感障碍的基本概念、症状表现及其治疗方法。第3章平台需求分析与设计详细分析用户需求,设计双相情感障碍交流平台的整体架构和功能模块。3.1用户需求分析通过调研了解用户需求,为平台设计提供参考。3.2平台整体架构设计根据用户需求,设计平台的整体架构和技术路线。3.3功能模块设计详细介绍平台各个功能模块的设计思路和实现方法。第4章平台实现与测试阐述双相情感障碍交流平台的实现过程,包括前端和后端的实现,以及平台的测试。4.1前端实现利用Vue框架实现平台的前端界面和交互功能。4.2后端实现采用Django框架实现平台的后端逻辑和数据处理功能。4.3平台测试与优化对平台进行全面测试,发现并解决潜在问题,优化平台性能。第5章平台应用与评价将双相情感障碍交流平台应用于实际场景,并对其进行综合评价。5.1平台应用情况介绍平台在实际应用中的情况,包括用户反馈和使用效果。5.2平台评价指标与方法确定平台的评价指标和方法,对平台进行全面评估。5.3评价结果与分析根据评价指标和方法,得出平台的评价结果,并进行详细分析。第6章结论与展望总结论文的研究成果,分析双相情感障碍交流平台的优缺点,并展望未来的研
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 安捷伦IO Libraries Suite 15.5是由安捷伦科技公司推出的一款专业软件工具,广泛应用于科学研究、工程开发和数据采集等领域。该版本专为Windows XP系统设计,旨在满足仍在使用XP系统的用户需求。以下是该套件的主要功能、使用场景及研究开发应用的详细介绍。 仪器控制:该套件的核心功能是通过GPIB、USB、以太网等多种接口,实现对安捷伦品牌仪器设备(如示波器、逻辑分析仪、频谱分析仪等)的远程操作。 驱动程序支持:包含大量驱动程序,覆盖安捷伦广泛的硬件产品线,确保用户能轻松与不同仪器通信。 数据采集与处理:提供数据采集工具,支持实时数据获取、存储和分析,便于用户管理实验数据。 编程接口:支持VISA、Visual Basic、C++、LabVIEW等多种编程语言,允许用户自定义仪器控制逻辑,实现自动化测试和测量。 教育实验室:在高校物理、电子、通信等实验室,该套件可帮助学生高效进行实验操作。 产品研发:工程师在产品开发阶段,可利用该套件快速验证原型,测试性能指标。 生产测试:在生产线上,可通过自动化脚本控制多台仪器进行批量测试,提高生产效率。 信号分析:在电子工程领域,该套件可与安捷伦频谱分析仪配合,进行复杂信号分析和频谱监测。 嵌入式系统测试:在嵌入式系统开发中,可用于调试硬件接口,验证系统功能。 软件定义无线电(SDR):在SDR项目中,该套件可提供实时数据采集和处理能力,用于信号接收和发送控制。 该版本虽然专为Windows XP设计,但随着操作系统的发展,用户可能需要升级到更高版本以适应新环境。需要注意的是,部分较新的安捷伦仪器可能不再支持XP系统,因此在选择仪器时需谨慎。尽管如此,安捷伦IO Libraries Suite 15.5仍是一个强大且全面的工具,尤
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值