C # example of email sending

Source: Internet
Author: User
Tags mailmessage smtpclient

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

 
 
  1. UsingSystem;
  2. UsingSystem. collections;
  3. UsingSystem. text;
  4. UsingSystem. net;
  5. UsingSystem. net. mail;
  6.  
  7. NamespaceNetmailsend
  8. {
  9. ClassPrograme
  10. {
  11. Public Static VoidMain (String[] ARGs)
  12. {
  13. // Mail message 
  14. Mailmessage mymail =NewMailmessage ();
  15. Mymail. From =NewMailaddress (Test@gmail.com");
  16. Mymail. to. Add (NewMailaddress (Test@gmail.com"));
  17. Mymail. Subject ="Test";
  18. Mymail. subjectencoding = encoding. utf8;
  19. Mymail. Body ="You are successed .";
  20. Mymail. bodyencoding = encoding. utf8;
  21. Mymail. isbodyhtml =False;
  22. Mymail. Priority = mailpriority. High;
  23. Mymail. CC. Add (NewMailaddress (Test@gmail.com"));
  24. Mymail. bcc. Add (NewMailaddress (Test@gmail.com"));
  25. // SMTP client 
  26. Smtpclient sender =NewSmtpclient ();
  27. Sender. Host ="Smtp.gmail.com";
  28. Sender. Port = 587;
  29. Sender. Credentials =
  30. NewNetworkcredential (Test@gmail.com","Test");
  31. Sender. deliverymethod = smtpdeliverymethod. Network;
  32. Sender. enablessl =True;
  33. Try
  34. {
  35. Sender. Send (mymail );
  36. Console. writeline ("Success");
  37. }
  38. Catch(Exception E)
  39. {
  40. Console. writeline ("Failed. Exception: {0 }", E. Message );
  41. }
  42. Console. Write ("Press any key to quit ...");
  43. Console. readkey ();
  44. }
  45. }
  46. }
  47.  

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.

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.