cdo收取邮件_用CDO发邮件

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Text;7 usingSystem.Windows.Forms;8 usingSystem.Threading;9 usingSystem.IO;10 11 namespaceSendMail12 {13 publicpartialclassFormMain : Form14 {15 //用于发送邮件的线程16 Thread threadSending=null;17 //代表邮件的 CDO.Message对象18 CDO.Message oMsg=null;19 //邮件发送流20 ADODB.Stream msgStream=null;21 22 23 publicFormMain()24 {25 InitializeComponent();26 Control.CheckForIllegalCrossThreadCalls=false;27 }28 29 privatevoidbutton_Send_Click(objectsender, EventArgs e)30 {31 if(this.threadSending!=null&&this.threadSending.IsAlive)32 {33 this.label_Info.ForeColor=Color.Red;34 this.label_Info.Text="目前正有一邮件发送中";35 return;36 }37 else38 {39 ThreadStart ts=newThreadStart(this.SendMail);40 this.threadSending=newThread(ts);41 this.threadSending.Start();42 }43 }44 45 46 privatevoidSendMail()47 {48 this.label_Info.ForeColor=Color.Blue;49 this.label_Info.Text="正发送邮件“"+this.textBox_Subject.Text+"”";50 51 try52 {53 this.oMsg=newCDO.Message();54 this.oMsg.From=this.textBox_Form.Text;55 this.oMsg.To=this.textBox_To.Text;56 this.oMsg.Subject=this.textBox_Subject.Text;57 this.oMsg.HTMLBody="

"+this.richTextBox_MessageBody.Text+"";58 59 //添加附件60 stringfile=this.textBox_Attachment.Text.Trim();61 if(!String.IsNullOrEmpty(file))62 {63 if(System.IO.File.Exists(file))64 {65 this.oMsg.AddAttachment(file,"","");66 }67 else68 {69 this.label_Info.ForeColor=Color.Red;70 this.label_Info.Text="添加的附件不存在,发送失败!";71 return;72 }73 }74 75 76 CDO.IConfiguration iConfg=this.oMsg.Configuration;77 ADODB.Fields oFields=iConfg.Fields;78 79 oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2;80 oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value="caoxuanyu@sina.com";//修改这里,并保持发件人与这里的地址一致81 oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value="caoxuanyu@sina.com";//修改这里82 oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="caoxuanyu";//修改这里83 oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="4603173";//修改这里84 oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;85 //value=0 代表Anonymous验证方式(不需要验证)86 //value=1 代表Basic验证方式(使用basic (clear-text) authentication.87 //The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)88 //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)89 oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804;90 oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value="smtp.sina.com.cn";91 92 oFields.Update();93 this.oMsg.BodyPart.Charset="gb2312";94 this.oMsg.HTMLBodyPart.Charset="gb2312";95 96 97 this.msgStream=this.oMsg.GetStream();98 this.timer_Sending.Start();99 100 101 this.oMsg.Send();102 103 104 this.timer_Sending.Stop();105 }106 catch(Exception ex)107 {108 this.label_Info.ForeColor=Color.Red;109 this.label_Info.Text="发送邮件“"+this.textBox_Subject.Text+"” 失败:\n"+ex.Message;110 return;111 }112 113 this.label_Info.ForeColor=Color.Green;114 this.label_Info.Text="已将邮件“"+this.textBox_Subject.Text+"”  发送至:"+this.textBox_To.Text;115 116 oMsg=null;117 118 }119 120 privatevoidlinkLabel_View_LinkClicked(objectsender, LinkLabelLinkClickedEventArgs e)121 {122 if(this.openFileDialog1.ShowDialog()==DialogResult.OK)123 {124 this.textBox_Attachment.Text=this.openFileDialog1.FileName;125 }126 }127 128 privatevoidlinkLabel_AttachmentCanel_LinkClicked(objectsender, LinkLabelLinkClickedEventArgs e)129 {130 this.textBox_Attachment.Clear();131 }132 133 //监视发送邮件的进度134 privatevoidtimer_Sending_Tick(objectsender, EventArgs e)135 {136 this.label_Info.ForeColor=Color.Blue;137 this.label_Info.Text="正发送邮件“"+this.textBox_Subject.Text+"”";138 139 if(this.msgStream!=null)140 {141 //不正确的方法142 //this.label_Info.Text += this.msgStream.Position + "/" + this.msgStream.Size;143 }144 }145 }146 }147 148
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值