. NET email sending code-Zookeeper passed

Source: Internet
Author: User

Introduce namespace

Using System. Net. Mail;
Using System. Net;

Private static bool SendMail (string toAddress, string mailSubject, string mailBody)
{
MailAddress from = new MailAddress ("xiachufeng@126.com", "xiachufeng"); // set the sender mailbox and display name
MailAddress to = new MailAddress (toAddress, "gangge"); // set the recipient's mailbox and display name
MailMessage oMail = new MailMessage (from, to); // create a MailMessage object
OMail. Subject = mailSubject; // mail title
OMail. Body = mailBody; // mail content

OMail. Attachments. Add (new Attachment ("D: \ project.rar"); // Attachment
OMail. IsBodyHtml = true; // specifies the mail format. The HTML format is supported.
OMail. BodyEncoding = System. Text. Encoding. GetEncoding ("UTF-8"); // The Encoding used by the email
OMail. Priority = MailPriority. High; // set the Priority of the email to High.
// Sending email server
SmtpClient client = new SmtpClient ();
Client. Host = "smtp.126.com"; // specify the email server
Client. Credentials = new NetworkCredential ("xiachufeng@126.com", "your password"); // specify the server email and password
Try
{
Client. Send (oMail); // Send an email
}
Catch (Exception eall)
{
String str = eall. Message;
Return false;
}
Finally
{

OMail. Attachments. Dispose ();
OMail. Dispose (); // release resources
}
Return true;
}

/// <Summary>
/// Call to send an email
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void button#click (object sender, EventArgs e)
{

Bool B = SendMail ("duchenggang.accp@163.com", "hello", "Haha, do a test gang ");
If (B = true)
{
ClientScript. RegisterStartupScript (this. GetType (), "onclick", "<script> alert ('OK') </script> ");

}
Else
{
ClientScript. RegisterStartupScript (this. GetType (), "onclick", "<script> alert ('error') </script> ");

}
}

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.