For. net, it is very easy to send emails from 2.0. Next, I will give an example of sending an email in C # group. I have made some detailed annotations and hope to help some friends who need it.
// Introduce the namespace
Using system. net;
Using system. net. mail;
Smtpclient SMTP = new smtpclient (); // instantiate a smtpclient
SMTP. deliverymethod = smtpdeliverymethod. Network; // set the SMTP outbound method to Network
SMTP. enablessl = false; // whether SSL encryption is enabled on the SMTP server
SMTP. Host = "smtp.163.com"; // specify the SMTP server address
SMTP. Port = 25; // specify the SMTP server port. The default value is 25.
// If your SMTP server does not require identity authentication, use the following method.
SMTP. usedefacrecredentials = true;
// If authentication is required, use the following method
SMTP. Credentials = new networkcredential ("email account @ 163.com"," email password ");
Mailmessage Mm = new mailmessage (); // instantiate an email class
Mm. Priority = mailpriority. High; // mail priority, which can be low, normal, or high. Normally, normal is used.
Mm. From = new mailaddress ("email account @ 163.com"," really interesting ", encoding. getencoding (936 ));
// The Source of the email that the recipient sees;
// The first parameter is the sender's email address.
// The second parameter is the name displayed by the sender.
// The third parameter is the encoding used by the second parameter. If this parameter is not specified correctly, garbled characters are displayed after receiving the request.
// 936 is the codePage value of Simplified Chinese
Note:The preceding email source must be the same as the account used to log on to your mailbox. Otherwise, authentication will fail.
Mm. replyto = new mailaddress ("test_box@gmail.com", "my inbox", encoding. getencoding (936 ));
// Replyto indicates the default Receiving address when the recipient replies to the email, that is, you use one email but the other to receive the email.
// The meanings of the preceding two parameters are the same as those of from.
Mm. CC. Add ("a@163.com, B @163.com, c@163.com ");
// CC of the email, Supporting Group Sending. Separate multiple email addresses with commas (,).
// Of course, you can also use the full address, as shown below:
Mm. CC. Add (New mailaddress ("a@163.com", "cc a", encoding. getencoding (936 )));
Mm. CC. Add (New mailaddress ("B @163.com", "cc B", encoding. getencoding (936 )));
Mm. CC. Add (New mailaddress ("c@163.com", "cc C", encoding. getencoding (936 )));
Mm. bcc. Add ("d@163.com, e@163.com ");
// BCC of the email, Supporting Group Sending. Separate multiple email addresses with commas (,).
// Of course, you can also use the full address, as shown below:
Mm. CC. Add (New mailaddress ("d@163.com", "bcc d", encoding. getencoding (936 )));
Mm. CC. Add (New mailaddress ("e@163.com", "bcc e", encoding. getencoding (936 )));
Mm. Sender = new mailaddress ("xxx@xxx.com", "email sender", encoding. getencoding (936 ));
// You can set it as needed. This information is contained in the mail header, but it does not verify the validity or display it to the recipient.
// To be honest, I don't know what the actual effect is. You may ignore it or not.
Mm. to. Add ("g@163.com, h@163.com ");
// Recipient of the email, Supporting Group Sending. Separate multiple addresses with commas
// Of course, you can also use the full address to add
Mm. to. Add (New mailaddress ("g@163.com", "receiver G", encoding. getencoding (936 )));
Mm. to. Add (New mailaddress ("h@163.com", "receiver H", encoding. getencoding (936 )));
Mm. Subject = "this is the mail title"; // The Mail title
Mm. subjectencoding = encoding. getencoding (936 );
// This is very important. If your email title contains Chinese characters, you must specify it here. Otherwise, the recipient may receive Garbled text.
// 936 is the pagecode in simplified Chinese. If it is an English title, this sentence can be ignored.
Mm. isbodyhtml = true; // whether the email body is in HTML Format
Mm. bodyencoding = encoding. getencoding (936 );
// The email body encoding. If the encoding is incorrect, the recipient will receive garbled characters.
Mm. Body = "<font color =" red "> email test, haha </font> ";
// Body of the email
Mm. attachments. Add (new attachment (@ "D: a.doc", system. net. Mime. mediatypenames. application. rtf ));
// Add an attachment. The second parameter indicates the file type of the attachment. You do not need to specify
// You can add multiple attachments.
Mm. attachments. Add (new attachment (@ "D: B .doc "));
SMTP. Send (mm); // send an email. If no exception is returned, this is done.