代码改变世界

学习心得之:发送邮件(C#)

2005-03-01 22:13  胖子  阅读(524)  评论(0编辑  收藏  举报

参考下面的代码:

// 添加命名空间 
// using System.Web.Mail;
MailMessage Message=new MailMessage(); 
// 收件人地址
Message.To = this.txtReceiver.Text.Trim();
// 发件人地址
Message.From = this.txtSender.Text.Trim();
// 类型(Text为纯文本)
Message.BodyFormat = MailFormat.Text;
// 设置邮件的优先级 
Message.Priority = MailPriority.High;
// 主题
Message.Subject = "邮件主题";
// 内容
Message.Body = this.txtMailContent.Text.Trim();

// 添加身份验证功能(如果需要的话)
string username = "MailUserName"// 发送邮件的帐号的用户名
string password = "MailPassword";   // 发送邮件的帐号的密码
Message.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1");
Message.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendusername", username);
Message.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendpassword", password);

// 发送邮件的服务器
SmtpMail.SmtpServer 
= "smtp.163.com";

// 发送邮件
SmtpMail.Send( Message );