C # Use System. net. Mail to send emails. In this implementation, pay attention to the following issues? C # What are the specific instances for sending emails using system. net. Mail? Now let's start with the story.
1. system. net. mail is. recommended Methods in Versions later than NET 2.0. It solves some encoding and security verification problems, and overwrites the mailmessage class, providing more functions, this makes it easier to create mailmessage.
2. In system. net. Mail. mailmessage, The subjectencoding attribute is added, and methods in the to, CC, and BCC fields are added to facilitate mass mailing. In addition, a more object-oriented mailaddress class object is used to indicate the mail address.
3. Add the smtpclient class to system. net. Mail. This class contains methods and attributes in this application, and can mark secure links.
4. It is worth noting that the port used by SMTP (simple message transfer protocol) is 25, which is also provided by most mail service periods, but Gmail is different, gmail's latest port is 587, instead of the previous 465.
5. The Gmail server requires secure connections, so you must specify sender. enablessl = true.
6. In addition, there is a very important method in the smtpclient object, which is sendasync (). This method has been reloaded. Public void sendasync (mailmessage message, object usertoken) you must use mailmessage as the sending object and use usertoken to specify the method called during asynchronous operations. Public void sendasync (string from, string recipients, string subject, string body, object usertoken) can send emails directly without creating a mailmessage object. usertoken is the same as the previous function, public void sendasynccancel () cancels asynchronous operations to send emails.
C # example of sending mail using system. net. Mail
-
- UsingSystem;
-
- UsingSystem. collections;
-
- UsingSystem. text;
-
- UsingSystem. net;
-
- UsingSystem. net. mail;
-
-
- NamespaceNetmailsend
-
- {
-
- ClassPrograme
-
- {
-
- Public Static VoidMain (String[] ARGs)
-
- {
-
- // Mail message
-
- Mailmessage mymail =NewMailmessage ();
- Mymail. From =NewMailaddress (Test@gmail.com");
-
- Mymail. to. Add (NewMailaddress (Test@gmail.com"));
-
- Mymail. Subject ="Test";
-
- Mymail. subjectencoding = encoding. utf8;
-
- Mymail. Body ="You are successed .";
-
- Mymail. bodyencoding = encoding. utf8;
- Mymail. isbodyhtml =False;
-
- Mymail. Priority = mailpriority. High;
-
- Mymail. CC. Add (NewMailaddress (Test@gmail.com"));
-
- Mymail. bcc. Add (NewMailaddress (Test@gmail.com"));
-
- // SMTP client
-
- Smtpclient sender =NewSmtpclient ();
- Sender. Host ="Smtp.gmail.com";
-
- Sender. Port = 587;
-
- Sender. Credentials =
-
- NewNetworkcredential (Test@gmail.com","Test");
-
- Sender. deliverymethod = smtpdeliverymethod. Network;
-
- Sender. enablessl =True;
-
- Try
-
- {
-
- Sender. Send (mymail );
- Console. writeline ("Success");
-
- }
-
- Catch(Exception E)
-
- {
-
- Console. writeline ("Failed. Exception: {0 }", E. Message );
-
- }
-
- Console. Write ("Press any key to quit ...");
-
- Console. readkey ();
-
- }
-
- }
-
- }
-
C # the specific content of the email sent using system. net. Mail will be introduced to you here, hoping to help you understand and learn how to send emails. C.