. Net sends an email,. net
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.