C # Email sending method summary

Source: Internet
Author: User
Tags mailmessage smtpclient

 

The "System. Net. Mail" provided in. Net FrameWork 2.0 can be easily implemented. This article lists three methods for sending:
1. Use Localhost;
2. Use normal SMTP;
3. SMTP over SSL;

For example:

1. Use LocalHost

Public void SendMailLocalhost () {System. net. mail. mailMessage msg = new System. net. mail. mailMessage (); msg. to. add ("a@a.com"); msg. to. add ("B @ B .com");/** msg. to. add ("B @ B .com"); * msg. to. add ("B @ B .com"); * msg. to. add ("B @ B .com"); can be sent to multiple people */msg. CC. add ("c@c.com");/** msg. CC. add ("c@c.com"); * msg. CC. add ("c@c.com"); can be copied to multiple people */msg. from = new MailAddress ("a@a.com", "AlphaWu", System. text. encoding. UTF8);/* the preceding three parameters are respectively the sender address (which can be written as needed), the sender name, And the encoding */msg. subject = "this is a test email"; // mail title msg. subjectEncoding = System. text. encoding. UTF8; // The Message Title code msg. body = "email content"; // message content msg. bodyEncoding = System. text. encoding. UTF8; // The Message Content Encoding msg. isBodyHtml = false; // whether the message is an HTML message msg. priority = MailPriority. high; // mail priority SmtpClient client = new SmtpClient (); client. host = "localhost"; object userState = msg; try {client. sendAsync (msg, userState); // client is a little simpler. send (msg); MessageBox. show ("sent successfully");} catch (System. net. mail. smtpException ex) {MessageBox. show (ex. message, "sending email error ");}}

2. Use normal SMTP

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.