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


    准备工作:

    从C:WindowsSystem32将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();
    
    
            }

    https://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx

  • 相关阅读:
    解构操作的使用
    【Spring Boot】spring boot环境配置
    【Java】windows下直接运行Java程序(利用bat运行jar文件)
    【JavaScript】数学计算的函数与数字的格式化
    【python】控制台python代码导出成exe文件
    【centOS】centOS安装图形界面
    【网站】python的Windows平台的扩展包
    【Django】(4)创建其他页面
    【Django】(3)创建网页:学习笔记主页
    【Django】(2)Django模块创建应用程序
  • 原文地址:https://www.cnblogs.com/bfyx/p/4745530.html
Copyright © 2020-2023  润新知