用CDO发邮件

1,关于System.Net.Mail:
    首先,不要寄希望于.net中的该名字空间(或其他旧版的名字空间),因为它不提供密码验证,这样你就使用不了邮件服务器。我们将使用CDO,在C:\WINDOWS\system32\下有个叫cdosys.dll的动态链接库文件,将它复制出来,并在你的程序中引用它。

2,关于邮件服务器:
    大家一定听说过Pop3,Smtp之类的名词,这是两种类型的邮件服务器,能够让你注册并使用他们邮件服务的大大小小的网站都有他们自己的邮件服务器,但并非每个都那么慷慨地免费提供给我们的这个小程序使用,Yahoo!不可以,但163的可以,也就是说,为了完成我们这个程序,你应该注册一个163邮件或找到其他免费的。我们假设你的邮件地址是"abc@163.com",密码为"yourpassword"

3,CDO.Message对象:
    代表了我们要发送的邮件对象。CDO.Message msg = new Message();
    msg.Form:发件人邮件地址
    msg.To:收件人邮件地址
    msg.Subject:邮件标题
    msg.HtmlBody:邮件主体,比如"<html><body>" + "邮件信息" + "</body></html>";
    msg.AddAttachment():添加附件
    msg.Send():发送邮件
4,其他设置:
 1   CDO.IConfiguration iConfg = msg.Configuration;
 2                 ADODB.Fields oFields = iConfg.Fields;
 3
 4                 oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
 5                 oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "abc@163.com";
 6                 oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "abc@163.com";
 7                 oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "abc";
 8                 oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "yourpassword";
 9                 oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
10                 oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
11                 oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.163.com";
12
13                 oFields.Update();
14                 this.oMsg.BodyPart.Charset = "gb2312";
15                 this.oMsg.HTMLBodyPart.Charset = "gb2312";
5,成果:
sendMail_pic.bmp

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using System.Threading;
  9 using System.IO;
 10 
 11 namespace SendMail
 12 {
 13     public partial class FormMain : Form
 14     {
 15         //用于发送邮件的线程
 16         Thread threadSending = null;
 17         //代表邮件的 CDO.Message对象
 18         CDO.Message oMsg = null;
 19         //邮件发送流
 20         ADODB.Stream msgStream = null;
 21        
 22 
 23         public FormMain()
 24         {
 25             InitializeComponent();
 26             Control.CheckForIllegalCrossThreadCalls = false;
 27         }
 28 
 29         private void button_Send_Click(object sender, 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             else
 38             {
 39                 ThreadStart ts = new ThreadStart(this.SendMail);
 40                 this.threadSending = new Thread(ts);
 41                 this.threadSending.Start();
 42             }
 43         }
 44 
 45 
 46         private void SendMail()
 47         {
 48             this.label_Info.ForeColor = Color.Blue;
 49             this.label_Info.Text = "正发送邮件“ " + this.textBox_Subject.Text + "";
 50 
 51             try
 52             {
 53                 this.oMsg = new CDO.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 = "<html><body>" + this.richTextBox_MessageBody.Text + "</body></html>";
 58 
 59                 //添加附件
 60                 string file = 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                     else
 68                     {
 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         private void linkLabel_View_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
121         {
122             if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
123             {
124                 this.textBox_Attachment.Text = this.openFileDialog1.FileName;
125             }
126         }
127 
128         private void linkLabel_AttachmentCanel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
129         {
130             this.textBox_Attachment.Clear();
131         }
132 
133         //监视发送邮件的进度
134         private void timer_Sending_Tick(object sender, 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 
原文地址:https://www.cnblogs.com/cxy521/p/1048802.html