首先要引入using System.Web.Mail;命名空间
主要代码如下:
string sReturn = string.Empty;
MailMessage mailMsg = new MailMessage();
mailMsg.BodyFormat = MailFormat.Html;
mailMsg.To = "ch@yahoo.com.cn";
mailMsg.From = "ch@sohu.com";
mailMsg.Subject = "这是老夫给你发的邮件,收到请回答~";
mailMsg.Body = "这就是内容啊.";
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "这里是发信人邮箱");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "这里是密码");
//
SmtpMail.SmtpServer ="smtp.sohu.com";//smpt服务器
try
{
SmtpMail.Send(mailMsg);
this.Label1.Text = "发送成功";
}
catch (Exception err)
{
this.Label2.Text = "<font color=red>发送失败" + err.Message.ToString() + "</font>";
}