. Net mail

Source: Internet
Author: User
Tags email account mailmessage smtpclient

Using system. net;
Using system. net. mail;

Smtpclient SMTP = new smtpclient (); // instantiate a smtpclientsmtp. deliverymethod = smtpdeliverymethod. network; // set the SMTP outbound method to networksmtp. 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, which is 25 by default. If the default port is used, skip this step.
// If your SMTP server does not require identity authentication, use the following method. usedefaultcredentials = true; // If authentication is required, use the following SMTP method. 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"," interesting ", encoding. getencoding (936); // The email source seen by the recipient; // The first parameter is the sender's email address // The second parameter is the sender's display name // The third parameter is the encoding used by the second parameter. If the specified parameter is incorrect, after receiving the code, the other party displays garbled characters. // 936 indicates the codePage value in 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 an email to send a mail, however, the meaning of the last two parameters is used to receive the email. The meaning of the two parameters is the same as that of the from parameter.
Mm. cc. add ("a@163.com, B @163.com, c@163.com"); // mail CC, support group, multiple mail addresses separated by commas // Of course, you can also use the full address, 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"); // mail BCC, support group, multiple mail addresses separated by 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 tell the truth, I don't know how it works. You can ignore it or not.
Mm. to. add ("g@163.com, h@163.com"); // recipient of the mail, supporting group, separated by commas between multiple addresses // 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 mail body is in HTML format mm. bodyencoding = encoding. getencoding (936); // The email body encoding. If the encoding is incorrect, the recipient will receive the garbled mm. body = "<font color =" red "> email test, haha </font>"; // mail body
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 the attachment. // you can add multiple attachments to mm. attachments. add (new attachment (@ "D: B .doc "));
SMTP. Send (mm); // send an email. If no exception is returned, this is done.

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.