.NET 使用C#两种发送邮件的方法

时间:2022-09-24 13:10:02

第一种:

  1、配置文件(Web.config) + 少量C#代码

  在 configuration 节点下添加

  <system.net>    <mailSettings>      <smtp from="baidu@163.com">        <network host="smtp.163.com" password="baidu" port="25" userName="baidu" defaultCredentials="false"/>      </smtp>    </mailSettings>  </system.net>

    /// <summary>

    /// 发送邮件/// </summary>private void SendMail(){    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();    message.From = new System.Net.Mail.MailAddress("baidu@163.com");//发送人邮箱地址,与smtp节点中的from值一致    message.To.Add(new System.Net.Mail.MailAddress("taobao@163.com"));//接收人邮箱地址    message.Subject = "hello";    message.Body = "<b>taobao</b>";    message.IsBodyHtml = true;    System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();    smtpclient.Send(message);}

    好,介绍完第一种发送邮件的方法,下面看第二种。

  /// <summary>

    /// 发送邮件/// </summary>private void SendMail(){    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();    message.From = new System.Net.Mail.MailAddress("baidu@163.com");//发送人邮箱地址,与smtp节点中的from值一致    message.To.Add(new System.Net.Mail.MailAddress("taobao@163.com"));//接收人邮箱地址    message.Subject = "hello";    message.Body = "<b>taobao</b>";    message.IsBodyHtml = true;    System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient("smtp.163.com", 25);    smtpclient.Credentials = new System.Net.NetworkCredential("baidu", "baidu");//参数分别是邮箱用户名和密码    smtpclient.Send(message);}