asp.net发送邮件,不用第三方哦~~~~

首先要引入using System.Web.Mail;命名空间

主要代码如下:

   string sReturn = string.Empty;
   MailMessage mailMsg = new MailMessage();
   mailMsg.BodyFormat = MailFormat.Html;
   mailMsg.To = "[email protected]";
   mailMsg.From = "[email protected]";
   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 = "发送失败" + err.Message.ToString() + "";
   }

 

你可能感兴趣的:(代码)