asp.net發送郵件代碼

來源:互聯網
上載者:User

asp教程.net發送郵件代碼

MailMessage mailMsg = new MailMessage(); 
  //設定收件者的郵件地址 
  mailMsg.To = "bailichunwow@qq.com "; 
   
  //設定寄件者的郵件地址 
  mailMsg.From = "bailichun@vip.qq.com "; 
  //設定郵件主題 
  mailMsg.Subject = "測試 "; 
   
  //設定郵件內容 
  mailMsg.Body = "內容 ";
  mailMsg.BodyFormat = MailFormat.Text;
  mailMsg.Priority = MailPriority.Normal;
  try
  {
  //設定發送郵件伺服器 
  SmtpMail.SmtpServer = "localhost";
  //發送郵件 
  SmtpMail.Send(mailMsg);

  }
  catch
  {

  }
 
//一款完整發送郵件代碼
MailObj _mail = new MailObj();
        _mail.sendMail("lxx@qq.com", "測試", "<b>內容</b>");
        _mail.Dispose();

//核心代碼

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;

namespace EC
{
    /// <summary>
    ///郵件發送
    /// </summary>
    public class MailObj
    {
        private string _strHost = string.Empty;
        private string _strAccount = string.Empty;
        private string _strPwd = string.Empty;
        private string _strFrom = string.Empty;

        #region 構造與解構函式
        public MailObj()
        {
            _strHost = "smtp.163.com";   //STMP伺服器位址
            _strAccount = "aa";       //SMTP服務帳號
            _strPwd = "123456";       //SMTP服務密碼
            _strFrom = "aa@163.com";  //發送方郵件地址
        }

        /// <summary>
        /// 發送郵件購造函數
        /// </summary>
        /// <param name="strHost">STMP伺服器位址:smtp.163.com</param>
        /// <param name="strAccount">SMTP服務帳號:liugongxun</param>
        /// <param name="strPwd">SMTP服務密碼:123456</param>
        /// <param name="strFrom">發送方郵件地址:liugongxun@163.com</param>
        public MailObj(string strHost, string strAccount, string strPwd, string strFrom)
        {
            _strHost = strHost;
            _strAccount = strAccount;
            _strPwd = strPwd;
            _strFrom = strFrom;
        }


        ~MailObj()
        {
            Dispose();
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        #endregion

        #region 發送郵件
        public bool sendMail(string to, string title, string content)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發送方式
            _smtpClient.Host = _strHost;//指定SMTP伺服器
            _smtpClient.Credentials = new System.Net.NetworkCredential(_strAccount, _strPwd);//使用者名稱和密碼

            MailMessage _mailMessage = new MailMessage(_strFrom, to);
            _mailMessage.Subject = title;//主題
            _mailMessage.Body = content;//內容
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//本文編碼
            _mailMessage.IsBodyHtml = true;//設定為HTML格式
            _mailMessage.Priority = MailPriority.High;//優先順序
            try
            {
                _smtpClient.Send(_mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.