Microsoft .NET Framework 2.0实现发送邮件(Email)总结
在 .NET Framework 2.0的System.Net.Mail命名空间中新增的MailMessage类和SmtpClient类使我们很容易发送Email,SmtpClient类提供了同步和异步2种方式发送邮件.
同步发送邮件由于比较简单,请看代码
Code
MailMessage mm = new MailMessage ( );
//发件人
mm.From = new MailAddress ( "aierong@126.com" , "aierong_126" );
//收件人(可以是多个)
mm.To.Add ( new MailAddress ( "aierong@qq.com" ) );
mm.To.Add ( new MailAddress ( "aierong@163.com" ) );
//抄送 (CC)(可以是多个)
mm.CC.Add ( new MailAddress ( "aierong@qq.com" ) );
mm.CC.Add ( new MailAddress ( "aierong@qq.com" ) );
//密件抄送 (BCC)(可以是多个)
mm.Bcc.Add ( new MailAddress ( "114298@qq.com" ) );
mm.Bcc.Add ( new MailAddress ( "10249102@qq.com" ) );
//主题
mm.Subject = "邮件的主题1";
//邮件正文
mm.Body = "body";
//附件(可以是多个)
mm.Attachments.Add ( new Attachment ( @"D:\Access.zip" ) );
mm.Attachments.Add ( new Attachment ( @"D:\SetupReport.msi" ) );
SmtpClient sc = new SmtpClient ( );
//设置SMTP主机的名称或IP地址
sc.Host = "smtp.126.com";
string User = "";
string PassWord = "";
//设置用于验证发件人身份的凭据
sc.Credentials = new System.Net.NetworkCredential ( User , PassWord );
sc.Send ( mm );
MessageBox.Show ( "ok" );
MailMessage mm = new MailMessage ( );
//发件人
mm.From = new MailAddress ( "aierong@126.com" , "aierong_126" );
//收件人(可以是多个)
mm.To.Add ( new MailAddress ( "aierong@qq.com" ) );
mm.To.Add ( new MailAddress ( "aierong@163.com" ) );
//抄送 (CC)(可以是多个)
mm.CC.Add ( new MailAddress ( "aierong@qq.com" ) );
mm.CC.Add ( new MailAddress ( "aierong@qq.com" ) );
//密件抄送 (BCC)(可以是多个)
mm.Bcc.Add ( new MailAddress ( "114298@qq.com" ) );
mm.Bcc.Add ( new MailAddress ( "10249102@qq.com" ) );
//主题
mm.Subject = "邮件的主题1";
//邮件正文
mm.Body = "body";
//附件(可以是多个)
mm.Attachments.Add ( new Attachment ( @"D:\Access.zip" ) );
mm.Attachments.Add ( new Attachment ( @"D:\SetupReport.msi" ) );
SmtpClient sc = new SmtpClient ( );
//设置SMTP主机的名称或IP地址
sc.Host = "smtp.126.com";
string User = "";
string PassWord = "";
//设置用于验证发件人身份的凭据
sc.Credentials = new System.Net.NetworkCredential ( User , PassWord );
sc.Send ( mm );
MessageBox.Show ( "ok" );
要是邮件容量比较大,可以设置SmtpClient.Timeout属性(默认值为100 秒),以免超时。
当然,我们还可以用配置文件来指定SmtpClient对象的默认主机、端口和凭据值等属性。请看下面配置
Code
<system.net>
<mailSettings>
<smtp>
<network password ="" userName ="" host ="smtp.126.com"/>
</smtp>
</mailSettings>
</system.net>
<system.net>
<mailSettings>
<smtp>
<network password ="" userName ="" host ="smtp.126.com"/>
</smtp>
</mailSettings>
</system.net>
异步发送邮件:
以上代码是基于同步的发送方式,利用SmtpClient.SendAsync方法可以很方便实现异步发送邮件
SendAsync方法描述如下:
public void SendAsync (
MailMessage message,
Object userToken
)
参数
message
包含要发送的消息的 MailMessage。
userToken
一个用户定义对象,此对象将被传递给完成异步操作时所调用的方法。
代码
Code
MailMessage mm = new MailMessage ( );
//发件人
mm.From = new MailAddress ( "aierong@126.com" , "aierong_126" );
//收件人(可以是多个)
mm.To.Add ( new MailAddress ( "aierong@qq.com" ) );
//主题
mm.Subject = "邮件的主题1";
//邮件正文
mm.Body = "body";
//附件(可以是多个)
mm.Attachments.Add ( new Attachment ( @"D:\Access.zip" ) );
SmtpClient sc = new SmtpClient ( );
//SmtpClient.SendCompleted事件,在异步电子邮件发送操作完成时调用
sc.SendCompleted += new SendCompletedEventHandler ( sc_SendCompleted );
//设置SMTP主机的名称或IP地址
sc.Host = "smtp.126.com";
string User = "aierong";
string PassWord = "aie100le";
//设置用于验证发件人身份的凭据
sc.Credentials = new System.Net.NetworkCredential ( User , PassWord );
//异步发送邮件
sc.SendAsync ( mm , "test message" );
void sc_SendCompleted ( object sender , AsyncCompletedEventArgs e )
{
if ( e.Error == null )
{
MessageBox.Show ( "发送成功" );
//显示出来用户定义对象"test message"
MessageBox.Show ( e.UserState.ToString() );
}
else
{
MessageBox.Show ( e.Error.Message );
}
}
MailMessage mm = new MailMessage ( );
//发件人
mm.From = new MailAddress ( "aierong@126.com" , "aierong_126" );
//收件人(可以是多个)
mm.To.Add ( new MailAddress ( "aierong@qq.com" ) );
//主题
mm.Subject = "邮件的主题1";
//邮件正文
mm.Body = "body";
//附件(可以是多个)
mm.Attachments.Add ( new Attachment ( @"D:\Access.zip" ) );
SmtpClient sc = new SmtpClient ( );
//SmtpClient.SendCompleted事件,在异步电子邮件发送操作完成时调用
sc.SendCompleted += new SendCompletedEventHandler ( sc_SendCompleted );
//设置SMTP主机的名称或IP地址
sc.Host = "smtp.126.com";
string User = "aierong";
string PassWord = "aie100le";
//设置用于验证发件人身份的凭据
sc.Credentials = new System.Net.NetworkCredential ( User , PassWord );
//异步发送邮件
sc.SendAsync ( mm , "test message" );
void sc_SendCompleted ( object sender , AsyncCompletedEventArgs e )
{
if ( e.Error == null )
{
MessageBox.Show ( "发送成功" );
//显示出来用户定义对象"test message"
MessageBox.Show ( e.UserState.ToString() );
}
else
{
MessageBox.Show ( e.Error.Message );
}
}
/************************************************/
本博客内容如果是原著都会在标题后加上(原著)字样,未加者多数为转载.
/************************************************/