利用asp.net怎么實(shí)現(xiàn)一個(gè)郵件發(fā)送功能-創(chuàng)新互聯(lián)

利用asp.net怎么實(shí)現(xiàn)一個(gè)郵件發(fā)送功能?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)公司從2013年成立,先為峨眉山等服務(wù)建站,峨眉山等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為峨眉山企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

具體實(shí)現(xiàn)方法如下:

MailMessage mailMsg = new MailMessage();


  //設(shè)置收件人的郵件地址 
  mailMsg.To = "bailichunwow@qq.com "; 
   
  //設(shè)置發(fā)送者的郵件地址 
  mailMsg.From = "bailichun@vip.qq.com "; 
  //設(shè)置郵件主題 
  mailMsg.Subject = "測(cè)試 "; 
   
  //設(shè)置郵件內(nèi)容 
  mailMsg.Body = "內(nèi)容 ";
  mailMsg.BodyFormat = MailFormat.Text;
  mailMsg.Priority = MailPriority.Normal;
  try
  {
  //設(shè)置發(fā)送郵件服務(wù)器 
  SmtpMail.SmtpServer = "localhost";
  //發(fā)送郵件 
  SmtpMail.Send(mailMsg);
  }
  catch
  {
  }
 
//一款完整發(fā)送郵件代碼
MailObj _mail = new MailObj();
        _mail.sendMail("lxx@qq.com", "測(cè)試", "<b>內(nèi)容</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>
    ///郵件發(fā)送
    /// </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 構(gòu)造與析構(gòu)函數(shù)
        public MailObj()
        {
            _strHost = "smtp.163.com";   //STMP服務(wù)器地址
            _strAccount = "aa";       //SMTP服務(wù)帳號(hào)
            _strPwd = "123456";       //SMTP服務(wù)密碼
            _strFrom = "aa@163.com";  //發(fā)送方郵件地址
        }
        /// <summary>
        /// 發(fā)送郵件購(gòu)造函數(shù)
        /// </summary>
        /// <param name="strHost">STMP服務(wù)器地址:smtp.163.com</param>
        /// <param name="strAccount">SMTP服務(wù)帳號(hào):liugongxun</param>
        /// <param name="strPwd">SMTP服務(wù)密碼:123456</param>
        /// <param name="strFrom">發(fā)送方郵件地址: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 發(fā)送郵件
        public bool sendMail(string to, string title, string content)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發(fā)送方式
            _smtpClient.Host = _strHost;//指定SMTP服務(wù)器
            _smtpClient.Credentials = new System.Net.NetworkCredential(_strAccount, _strPwd);//用戶名和密碼
            MailMessage _mailMessage = new MailMessage(_strFrom, to);
            _mailMessage.Subject = title;//主題
            _mailMessage.Body = content;//內(nèi)容
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文編碼
            _mailMessage.IsBodyHtml = true;//設(shè)置為HTML格式
            _mailMessage.Priority = MailPriority.High;//優(yōu)先級(jí)
            try
            {
                _smtpClient.Send(_mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}


看完上述內(nèi)容,你們掌握利用asp.net怎么實(shí)現(xiàn)一個(gè)郵件發(fā)送功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)頁(yè)標(biāo)題:利用asp.net怎么實(shí)現(xiàn)一個(gè)郵件發(fā)送功能-創(chuàng)新互聯(lián)
分享鏈接:http://muchs.cn/article26/hiicg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、微信小程序靜態(tài)網(wǎng)站、網(wǎng)站改版網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化