[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


1 Public void sendmaillocalhost ()
2 ...{
3 system. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage ();
4 msg. to. Add ("a@a.com ");
5 msg. to. Add ("B @ B .com ");
6 /**//*
7 * msg. to. Add ("B @ B .com ");
8 * msg. to. Add ("B @ B .com ");
9 * msg. to. Add ("B @ B .com"); can be sent to multiple people
10 */
11 msg. CC. Add ("c@c.com ");
12 /**//*
13 * msg. CC. Add ("c@c.com ");
14 * msg. CC. Add ("c@c.com"); can be copied to multiple people
15 */
16 MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 );
17/** // * the preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */
18 MSG. Subject = "this is a test email"; // mail title
19 msg. subjectencoding = system. Text. encoding. utf8; // email title Encoding
20 msg. Body = "Mail content"; // mail content
21 msg. bodyencoding = system. Text. encoding. utf8; // email Content Encoding
22 msg. isbodyhtml = false; // whether it is an HTML email
23 MSG. Priority = mailpriority. High; // mail priority
24
25 smtpclient client = new smtpclient ();
26 client. Host = "localhost ";
27 object userstate = MSG;
28 try
29 ...{
30 client. sendasync (MSG, userstate );
31 // you can simply use client. Send (MSG );
32 MessageBox. Show ("sent successfully ");
33}
34 catch (system. net. Mail. smtpexception ex)
35 ...{
36 MessageBox. Show (ex. message, "email sending error ");
37}
38}
39


2. Use normal SMTP


1 Public void sendmailusezj ()
2 ...{
3 system. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage ();
4 msg. to. Add ("a@a.com ");
5 msg. to. Add ("B @ B .com ");
6 /**//*
7 * msg. to. Add ("B @ B .com ");
8 * msg. to. Add ("B @ B .com ");
9 * msg. to. Add ("B @ B .com"); can be sent to multiple people
10 */
11 msg. CC. Add ("c@c.com ");
12 /**//*
13 * msg. CC. Add ("c@c.com ");
14 * msg. CC. Add ("c@c.com"); can be copied to multiple people
15 */
16 MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 );
17/** // * the preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */
18 MSG. Subject = "this is a test email"; // mail title
19 msg. subjectencoding = system. Text. encoding. utf8; // email title Encoding
20 msg. Body = "Mail content"; // mail content
21 msg. bodyencoding = system. Text. encoding. utf8; // email Content Encoding
22 msg. isbodyhtml = false; // whether it is an HTML email
23 MSG. Priority = mailpriority. High; // mail priority
24
25 smtpclient client = new smtpclient ();
26 client. Credentials = new system. net. networkcredential ("username@zj.com", "userpass ");
27 // the email address and password registered at zj.com
28 client. Host = "smtp.zj.com ";
29 object userstate = MSG;
30 try
31 ...{
32 client. sendasync (MSG, userstate );
33 // you can simply use client. Send (MSG );
34 MessageBox. Show ("sent successfully ");
35}
36 catch (system. net. Mail. smtpexception ex)
37 ...{
38 MessageBox. Show (ex. message, "email sending error ");
39}
40}
41


The above method is not applicable to all SMTP, and zj.com can be used after testing, but smtp.163.com cannot.


3. SMTP over SSL


1 Public void sendmailusegmail ()
2 ...{
3 system. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage ();
4 msg. to. Add ("a@a.com ");
5 msg. to. Add ("B @ B .com ");
6 /**//*
7 * msg. to. Add ("B @ B .com ");
8 * msg. to. Add ("B @ B .com ");
9 * msg. to. Add ("B @ B .com"); can be sent to multiple people
10 */
11 msg. CC. Add ("c@c.com ");
12 /**//*
13 * msg. CC. Add ("c@c.com ");
14 * msg. CC. Add ("c@c.com"); can be copied to multiple people
15 */
16 MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 );
17/** // * the preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */
18 MSG. Subject = "this is a test email"; // mail title
19 msg. subjectencoding = system. Text. encoding. utf8; // email title Encoding
20 msg. Body = "Mail content"; // mail content
21 msg. bodyencoding = system. Text. encoding. utf8; // email Content Encoding
22 msg. isbodyhtml = false; // whether it is an HTML email
23 MSG. Priority = mailpriority. High; // mail priority
24
25 smtpclient client = new smtpclient ();
26 client. Credentials = new system. net. networkcredential ("username@gmail.com", "password ");
27 // write your Gmail mailbox and password above
28 client. Port = 587; // port used by Gmail
29 client. Host = "smtp.gmail.com ";
30 client. enablessl = true; // encrypted by SSL
31 object userstate = MSG;
32 try
33 ...{
34 client. sendasync (MSG, userstate );
35 // you can simply use client. Send (MSG );
36 MessageBox. Show ("sent successfully ");
37}
38 catch (system. net. Mail. smtpexception ex)
39 ...{
40 MessageBox. Show (ex. message, "email sending error ");
41}
42}
43
Using Gmail to send emails, the success rate is extremely high, and almost all emails can be sent. It is recommended.

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.