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;
}
}
}
C#中使用CDO.Message发送邮件,成功率极高
猜你喜欢
转载自blog.csdn.net/smartsmile2012/article/details/79438559
今日推荐
周排行