asp.net send mail Implementation method _ Practical skills

Source: Internet
Author: User
Tags mail code mailmessage net send smtpclient

The example of this article describes the implementation of ASP.net sending mail. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:

MailMessage MAILMSG = new MailMessage ();
Set the recipient's e-mail address
mailmsg.to = "bailichunwow@qq.com";

Set the sender's e-mail address
Mailmsg.from = "bailichun@vip.qq.com";
Set Message subject
Mailmsg.subject = "Test";

Set up message content
Mailmsg.body = "Content";
Mailmsg.bodyformat = Mailformat.text;
mailmsg.priority = Mailpriority.normal;
Try
{
Setting up a Send mail server
Smtpmail.smtpserver = "localhost";
Send mail
Smtpmail.send (MAILMSG);
}
Catch
{
}

A complete Send mail 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 mail
</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 Construction and destructor function
Public Mailobj ()
{
_strhost = "smtp.163.com"; STMP server Address
_straccount = "AA"; SMTP Service Account
_strpwd = "123456"; SMTP Service password
_strfrom = "aa@163.com"; Mailing address of sending party
}
<summary>
Send mail purchase function
</summary>
<param name= "Strhost" >stmp server address:smtp.163.com</param>
<param name= "Straccount" &GT;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 mail
public bool SendMail (string to, string title, string content)
{
SmtpClient _smtpclient = new SmtpClient ();
_smtpclient.deliverymethod = smtpdeliverymethod.network;//Specify how e-mail is sent
_smtpclient.host = _strhost;//Specify SMTP server
_smtpclient.credentials = new System.Net.NetworkCredential (_straccount, _strpwd);//username and password
MailMessage _mailmessage = new MailMessage (_strfrom, to);
_mailmessage.subject = title;//Theme
_mailmessage.body = content;//Content
_mailmessage.bodyencoding = system.text.encoding.utf8;//Body encoding
_mailmessage.isbodyhtml = true;//set 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 with the ASP.net program design.

Related Article

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.