1. 先添加COM 引用 "Microsoft CDO for Windows 2000 Library"
2.代码
Code
public static void ProxySendMail()
{
CDO.Message msg = new CDO.Message();
string user="gpaft";
string passWord = "!QAZ1qaz";
string from = "gpaft@grouppower.local";
string to = "dingfeng_wu@163.com";
string reply="dingfeng_wu@yahoo.com.cn";
string server = "192.168.1.127";
msg.From = from;
msg.To = to;
msg.Subject = "代理发送测试";
msg.TextBody = "代理测试内容.";
CDO.IConfiguration iConfig = msg.Configuration;
ADODB.Fields fields = iConfig.Fields;
fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = from;
fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = passWord;
fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 2;
fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = server;
fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = user;
fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = reply;
fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = user;
fields.Update();
try
{
msg.Send();
msg = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
public static void ProxySendMail()
{
CDO.Message msg = new CDO.Message();
string user="gpaft";
string passWord = "!QAZ1qaz";
string from = "gpaft@grouppower.local";
string to = "dingfeng_wu@163.com";
string reply="dingfeng_wu@yahoo.com.cn";
string server = "192.168.1.127";
msg.From = from;
msg.To = to;
msg.Subject = "代理发送测试";
msg.TextBody = "代理测试内容.";
CDO.IConfiguration iConfig = msg.Configuration;
ADODB.Fields fields = iConfig.Fields;
fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = from;
fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = passWord;
fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 2;
fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = server;
fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = user;
fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = reply;
fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = user;
fields.Update();
try
{
msg.Send();
msg = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
注:
fields 的smtpauthenticate 属性的取值也是挺有意思的, 当它取0时, 代表无需验证, 1代表基本的验证, 2是NTLM验证.
sendUsing 取2 的意思是, 强制通过smtpServer 属性指定的server 发送邮件.server属性设为exchange server 的IP 地址.
经测试,只要smtpauthenticate 设1时可以通过.