asp.net to send mail implementation method

  • 2021-01-25 07:24:59
  • OfStack

This article illustrates the implementation of asp.net to send mail. Share with you for your reference. The specific implementation method is as follows:


MailMessage mailMsg = new MailMessage(); 
  // Set the recipient's email address  
  mailMsg.To = "bailichunwow@qq.com "; 
   
  // Sets the sender's email address  
  mailMsg.From = "bailichun@vip.qq.com "; 
  // Set the subject of the message  
  mailMsg.Subject = " test "; 
   
  // Setting Message Content  
  mailMsg.Body = " content ";
  mailMsg.BodyFormat = MailFormat.Text;
  mailMsg.Priority = MailPriority.Normal;
  try
  {
  // Set up the sending mail server  
  SmtpMail.SmtpServer = "localhost";
  // Send E-mail  
  SmtpMail.Send(mailMsg);
  }
  catch
  {
  }
 
//1 The complete sending email code
MailObj _mail = new MailObj();
        _mail.sendMail("lxx@qq.com", " test ", "<b> content </b>");
        _mail.Dispose();
// The core code
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>
    /// Mail delivery
    /// </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 Constructors and destructors
        public MailObj()
        {
            _strHost = "smtp.163.com";   //STMP Server address
            _strAccount = "aa";       //SMTP Service account
            _strPwd = "123456";       //SMTP Service password
            _strFrom = "aa@163.com";  // The sender's email address
        }
        /// <summary>
        /// Send mail purchase function
        /// </summary>
        /// <param name="strHost">STMP Server address :smtp.163.com</param>
        /// <param name="strAccount">SMTP Service account :liugongxun</param>
        /// <param name="strPwd">SMTP Service password :123456</param>
        /// <param name="strFrom"> The sender's email address :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 Send E-mail
        public bool sendMail(string to, string title, string content)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;// Specify how to send an email
            _smtpClient.Host = _strHost;// The specified SMTP The server
            _smtpClient.Credentials = new System.Net.NetworkCredential(_strAccount, _strPwd);// User name and password
            MailMessage _mailMessage = new MailMessage(_strFrom, to);
            _mailMessage.Subject = title;// The theme
            _mailMessage.Body = content;// content
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;// The body of the code
            _mailMessage.IsBodyHtml = true;// Set to HTML format
            _mailMessage.Priority = MailPriority.High;// priority
            try
            {
                _smtpClient.Send(_mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}

asp.net I hope this article is helpful to your asp.


Related articles: