日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C#使用CDO发送邮件

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#使用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();
}?
?

總結

以上是生活随笔為你收集整理的C#使用CDO发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。