c# 使用Codosys.dll(CDO)发送邮件

准备工作:

从C:\Windows\System32将Codosys.dll拷到你的项目里,然后引用,或者直接引用Com组件也可以

然后看代码

 ///<summary>
        /// 构造函数
        ///</summary>
        ///<param name="strserver">发件箱的邮件服务器地址</param>
        ///<param name="strtoMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
        ///<param name="strfromMail">发件人地址</param>
        ///<param name="strsubject">邮件标题</param>
        ///<param name="stremailBody">邮件内容(可以以html格式进行设计)</param>
        ///<param name="strpassword">发件人邮箱密码</param>
        ///<param name="intport">发送邮件所用的端口号(htmp协议默认为25)</param>
        ///<param name="blnsslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
        /// <param name="strCC">抄送人</param>
        /// <param name="files">附件</param>
        public static void SendMyEmailByCDO(string strserver,
            string strtoMail,
            string strfromMail,
            string strsubject,
            string stremailBody,
            string strpassword,
            int intport,
            bool blnsslEnable, string strCC, List<string> files)
        {
            string yourEmail = strfromMail;
            CDO.Message message = new CDO.Message();
            CDO.IConfiguration configuration = message.Configuration;
            ADODB.Fields fields = configuration.Fields;

            Console.WriteLine(String.Format("Configuring CDO settings..."));

            ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
            field.Value = strserver;

            field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
            field.Value = intport;


            field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
            field.Value = CDO.CdoSendUsing.cdoSendUsingPort;

            field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
            field.Value = CDO.CdoProtocolsAuthentication.cdoBasic;

            field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
            field.Value = yourEmail;

            field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
            field.Value = strpassword;

            field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
            field.Value = blnsslEnable.ToString().ToLower();

            fields.Update();

            Console.WriteLine(String.Format("Building CDO Message..."));

            message.From = yourEmail;
            message.To = strtoMail;
            message.Subject = strsubject;

            if (!string.IsNullOrEmpty(strCC))
            {
                message.CC = strCC;
            }

            message.HTMLBody = stremailBody;
            if (files != null)
            {
                foreach (string item in files)
                {
                    message.AddAttachment(item);
                }
            }

            Console.WriteLine(String.Format("Attempting to connect to remote server..."));

            // Send message.
            message.Send();


        }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券