欢迎光临
我们一直在努力

C#使用CDO发送邮件-.NET教程,C#语言

建站超值云服务器,限时71元/月

author:david euler

date: 2004/11/18

email:de_euler-david@yahoo.com.cn

有任何问题,请与我联系:)

一直想做实现一个程序,定期给自己发送邮件,或者给朋友发送邮件;比如在节日或者纪念日前若干天,发送邮件给自己提醒,或者朋友生日前夕发邮件提醒。找了很长时间,都没有找到可用的资料。

csdn上查到可以用cdo,有一篇文章说“在reference中添加cdo for windows 2000 ”,于是在引用里面找,也没有找到一个名字以cdo开头的组件,下午的时候仔细看了一下可以引用的com组件列表,发现里面有一个名为microsoft cdo for exchange 2000 library的com组件,就是这个,我们可以用它来连接smtp server,使用用户名/密码验证发送邮件。

下面是实现的一个例子:

smtp server使用的smtp-srv,登陆用户名是david euler,发送邮箱是davideuler@test.com,发送到test@test.com/

1).资源管理器里面,添加引用(reference),添加microsoft cdo for exchange 2000 library的com组件;

2).编辑用户界面如上图,依次添加fromtextbox,totextbox,cctextbox,bcctextbox,subjecttextbox,messagetextbox,passwordtextbox,smtptextbox,设置messagetextbox的textmode属性为“multiline“, passwordtextbox的textmode属性为“password“,并添加响应提示标签,添加发送按钮send。

3).输入用户名,密码,smtp server之后,用户点击send按钮发送邮件,

send 按钮的click事件代码如下:

cdo.message omsg = new cdo.message();

//omsg.from = fromtextbox.text ;

omsg.to = totextbox.text ;

omsg.subject = subjecttextbox.text ;

omsg.textbody = messagetextbox.text ;

omsg.cc=cctextbox.text ;

omsg.bcc=bcctextbox.text ;

string username;

string emailfrom;

string password=passwordtextbox.text.tostring().trim();

username=fromtextbox.text.trim();

emailfrom=username.replace(" ","")+"@test.com";

omsg.from=emailfrom;

cdo.iconfiguration iconfg;

adodb.fields ofields;

iconfg = omsg.configuration;

ofields = iconfg.fields;

ofields["http://schemas.microsoft.com/cdo/configuration/sendusing"].value=2;

ofields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].value=emailfrom;

ofields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].value=emailfrom;

ofields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].value=username;

ofields["http://schemas.microsoft.com/cdo/configuration/sendusername"].value=username;

ofields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].value=password;

ofields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].value=1;

ofields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].value=smtptextbox.text.trim(); //smtp.163.com

ofields.update();

try

{

omsg.send();

omsg = null;

response.write("<script>alert("+ "邮件发送成功!" +");</script>");

}

catch (exception ex)

{

response.write("<script>alert("+ "发送失败:" +");</script>");

string exmsg="username:"+username+

" passwd:"+password+

" smtp:"+smtptextbox.text.trim();

response.write("<script>alert("+ exmsg +");</script>");

failedlabel.text=ex.message.tostring();

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#使用CDO发送邮件-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章