C#中使用CDO.Message发送邮件,成功率极高

Asp.net & C# 专栏收录该内容
219 篇文章 1 订阅
using CDO;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string from = "xxx";
            string to = "xxx";
            string userName = from;
            string password = "xxx";
            string title = "测试邮件";
            string body = "Hello, how are you doing?";
            string server = "smtp.126.com";
            int port = 25;  //ssl 465
            try
            {
                SendEmail(from, to, title, body, password, server, port, false);
                Console.WriteLine("发送完成");
            }
            catch (Exception)
            {

                throw new Exception();
            }


            Console.ReadKey();
        }


        public static bool SendEmail(string from, string to, string title, string body, string password, string server, int port, bool ssl)
        {
            bool flag = false;
            try
            {
                #region
                Message oMsg = new Message();
                var conf = oMsg.Configuration;
                var oFields = conf.Fields;

                oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = CdoSendUsing.cdoSendUsingPort;
                oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = CdoProtocolsAuthentication.cdoBasic;
                if (ssl)
                {
                    oFields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = true;
                }
                oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = server;//必填,而且要真实可用   
                oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = port;//邮箱端口
                oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = from;//发送者邮箱
                oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = from;//邮箱发送者名称   
                oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;   //邮箱发送者密码,必须真实   
                oFields.Update();

                oMsg.Configuration = conf;
                oMsg.Subject = title;//主题
                oMsg.HTMLBody = body;//邮件正文                      
                oMsg.From = from;//发送者
                oMsg.To = to;//接收者
                oMsg.Send();//发送
                flag = true;
                #endregion
            }
            catch (Exception)
            {
                flag = false;
                throw new Exception();
            }
            return flag;
        }
    }
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值