Implementation of asp.net mail sending method and asp.net mail sending Method

Source: Internet
Author: User

Implementation of asp.net mail sending method and asp.net mail sending Method

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

Copy codeThe Code is as follows:
MailMessage mailMsg = new MailMessage ();
// Set the recipient's email address
MailMsg. To = "bailichunwow@qq.com ";

// Set the sender's email address
MailMsg. From = "bailichun@vip.qq.com ";
// Set the Email Subject
MailMsg. Subject = "test ";

// Set the email content
MailMsg. Body = "content ";
MailMsg. BodyFormat = MailFormat. Text;
MailMsg. Priority = MailPriority. Normal;
Try
{
// Set the email sending server
SmtpMail. SmtpServer = "localhost ";
// Send an email
SmtpMail. Send (mailMsg );
}
Catch
{
}
 
// A complete email sending code
MailObj _ mail = new MailObj ();
_ Mail. sendMail ("lxx@qq.com", "test", "<B> content </B> ");
_ Mail. Dispose ();
// 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>
/// Send email
/// </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 constructor and destructor
Public MailObj ()
{
_ StrHost = "smtp.163.com"; // address of the STMP Server
_ StrAccount = "aa"; // SMTP service account
_ StrPwd = "123456"; // SMTP service Password
_ StrFrom = "aa@163.com"; // the sender's email address
}
/// <Summary>
/// Send email 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"> sender 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 email
Public bool sendMail (string to, string title, string content)
{
SmtpClient _ smtpClient = new SmtpClient ();
_ SmtpClient. DeliveryMethod = SmtpDeliveryMethod. Network; // specify the email sending Method
_ SmtpClient. Host = _ strHost; // specify the SMTP server
_ SmtpClient. Credentials = new System. Net. NetworkCredential (_ strAccount, _ strPwd); // user name and password
MailMessage _ mailMessage = new MailMessage (_ strFrom, );
_ MailMessage. Subject = title; // Subject
_ MailMessage. Body = content; // content
_ MailMessage. BodyEncoding = System. Text. Encoding. UTF8; // body Encoding
_ MailMessage. IsBodyHtml = true; // set it to HTML Format
_ MailMessage. Priority = MailPriority. High; // Priority
Try
{
_ SmtpClient. Send (_ mailMessage );
Return true;
}
Catch
{
Return false;
}
}
# Endregion
}
}

I hope this article will help you design your asp.net program.


Send emails using aspnet

Contact me ~~
I have done this before

Private void Page_Load (object sender, System. EventArgs e)
{
String sReturn = string. Empty;
MailMessage mailMsg = new MailMessage ();
MailMsg. BodyFormat = MailFormat. Html;
MailMsg. To = "***** @ 163.com ";
MailMsg. From = "*** @ 163.com ";
MailMsg. Subject = "email. Subject ";
MailMsg. Body = "email. Body ";

// MailMsg. Fields. Add ("schemas.microsoft.com/...ticate", "1 ");
// MailMsg. Fields. Add ("schemas.microsoft.com/cdo/configuration/sendusername", "username ");
// MailMsg. Fields. Add ("schemas.microsoft.com/cdo/configuration/sendpassword", "password ");
//
SmtpMail. SmtpServer = "smtp.126.com"; // smpt Server
Try
{

SmtpMail. Send (mailMsg );
This. Label1.Text = "sent successfully ";
}
Catch (Exception err)
{
This. Label2.Text = "<font color = red> failed to send" + err. Message. ToString () + "</font> ";
}

}

C # How does aspnet send emails?

MailMessage mm = new MailMessage ();
Mm. BodyFormat = System. Web. Mail. MailFormat. Html;
Mm. From = "Enter the sender's email here, for example: (*** @ 163.com )";
Mm. Fields. Add ("schemas.microsoft.com/...ticate", "1 ");
Mm. Fields. Add ("schemas.microsoft.com/cdo/configuration/sendusername", "Enter the account of the sender's email address and the content before ");
Mm. Fields. Add ("schemas.microsoft.com/cdo/configuration/sendpassword", "Enter the sender's email password ");
Mm. To = "here is the email address of the recipient ";
Mm. Subject = "Enter the mail title here ";
Mm. Body = "Enter the email content here ";
System. Web. Mail. SmtpMail. SmtpServer = "smtp.163.com"; // sender server name
Try
{
System. Web. Mail. SmtpMail. Send (mm); // Send an email
}
Catch (System. Exception err)
{
Response. Write (err. Message );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.