Example of sending email in ASP. NET

Source: Internet
Author: User
Tags mailmessage smtpclient

First, import the namespace:

The Code is as follows:
Using System. Net. Mail;
 

Define the method for sending emails. [There are many differences on the Internet. For more information, see WinForm.]

The Code is as follows:
/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "MessageFrom"> sender's email address </param>
/// <Param name = "MessageTo"> recipient's email address </param>
/// <Param name = "MessageSubject"> subject </param>
/// <Param name = "MessageBody"> email content </param>
/// <Returns> </returns>
Public bool Send (MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
MailMessage message = new MailMessage ();
Message. From = MessageFrom;
Message. To. Add (MessageTo); // you can specify multiple recipient email addresses for group sending.
Message. Subject = MessageSubject;
Message. Body = MessageBody;
Message. IsBodyHtml = true; // whether it is in html Format
Message. Priority = MailPriority. Normal; // mail Priority
SmtpClient SC = new SmtpClient ();
SC. Host = "smtp.qq.com"; // specify the server address or IP address of the email.
SC. Port = 25; // specify the sending Port
// Specify the username and password for logging on to the server (sender's email login password)
SC. Credentials = new System. Net. NetworkCredential ("[Sending address]", "[www. bKjia. c0m sending Password ]");
Try
{
SC. Send (message); // Send an email
}
Catch
{
Return false;
}
Return true;
}
 

How to send an email

Call the defined method to send Emails:

The Code is as follows:
/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void ForMail (string name, string mail)
{
Try
{
// String email = txtemail. Text. Trim ();
MailAddress MessageFrom = new MailAddress ("[sender address]"); // sender's email address
String MessageTo = mail; // recipient's email address
String MessageSubject = bs. HtmlEncode (subject );
// Mail content (generally a URL link that generates a random number and adds the verification id parameter. Click Go to website verification .) ";
String MessageBody = "" + content1.Value. Trim () + "";
If (Send (MessageFrom, MessageTo, MessageSubject, MessageBody ))
{
// Response. Write ("email sent successfully ");
}
Else
{
// Response. Write ("failed to send mail ");
}
}
Catch
{
// ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script> alert ('client information deletion failed') </script> ");
}
}
 

Call method to send an email
 

Smtp addresses in different mailboxes are different. For example, smtp.qq.com for QQ and smtp.163.com for 163

For more details, see http://www.bKjia. c0m/net/57055.htm

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.