C # email sending (the most boring mailbox-QQ mailbox)

Source: Internet
Author: User
Tags mail exchange mailmessage smtpclient telnet program

I recently worked very leisurely. When I was free, I went out to play with my sister. I watched novels, watched movies, and had a good time. I regret it this week. The code is my favorite, write an email sending program when you do something small. I believe that the email sending mentioned above will basically be used after work. After the user registers, he will send a verification email to verify it. When the user changes the password, the email will verify it, how do users send an email to investigate the website experience? send an email to push the most popular content on the website recently. Well, it's a bit cool. Let's get started: SMTP defines the Simple Mail Transfer Protocol (SMTP), which is actually the standard for transferring emails over the Internet. SMTP is a relatively simple text-based protocol. One or more recipients of a message are specified on top of the message (in most cases, the message is confirmed to exist), and the message text is transmitted. You can use the telnet program to test an SMTP server. SMTP uses TCP port 25. To determine an SMTP server for a given domain name, you need to use MX (Mail eXchange) DNS. (From Wikipedia) A simple email sending program is used to send emails from various mailboxes on the Internet. Generally, it is a Gmail mailbox (which is quite popular among many programmers ), netease mailbox (I love posting, but I don't want to talk about it) and Sina mail (I have registered an interface with Sina before, and now I have become a selling place ), qq mail (which has the largest number of users, the widest distribution, and the most boring mailbox), the most commonly used mailbox is probably these kinds, there are also corresponding programs on the Internet, most of the writing is relatively messy, A buddy wrote an email class, and the original user did not know who it was. If he had never done so, he could use this class to train his hand ~ Copy code 1 public class Email 2 {3 /// <summary> 4 /// sender 5 /// </summary> 6 public string mailFrom {get; set ;} 7 8 /// <summary> 9 /// recipient 10 /// </summary> 11 public string [] mailToArray {get; set ;} 12 13 /// <summary> 14 /// CC 15 /// </summary> 16 public string [] mailCcArray {get; set ;} 17 18 /// <summary> 19 // Title 20 /// </summary> 21 public string mailSubject {get; set;} 22 23 // <su Mmary> 24 // body 25 /// </summary> 26 public string mailBody {get; set ;} 27 28 /// <summary> 29 // sender password 30 /// </summary> 31 public string mailPwd {get; set ;} 32 33 // <summary> 34 // SMTP Mail Server 35 /// </summary> 36 public string host {get; set ;} 37 38 // <summary> 39 // whether the body is in html format 40 // </summary> 41 public bool isbodyHtml {get; set ;} 42 43 // <summary> 44 // Annex 45 /// </summary> 46 public string [] attachmentsPath {get; set;} 47 48 public bool Send () 49 {50 // use the specified email address to initialize MailAddress instance 51 MailAddress maddr = new MailAddress (mailFrom); 52 // initialize MailMessage instance 53 MailMessage myMail = new MailMessage (); 54 55 56 // Add email address 57 if (mailToArray! = Null) 58 {59 for (int I = 0; I <mailToArray. length; I ++) 60 {61 myMail. to. add (mailToArray [I]. toString (); 62} 63} 64 65 // Add the email address 66 if (mailCcArray! = Null) 67 {68 for (int I = 0; I <mailCcArray. length; I ++) 69 {70 myMail. CC. add (mailCcArray [I]. toString (); 71} 72} 73 // sender address 74 myMail. from = maddr; 75 76 // email title 77 myMail. subject = mailSubject; 78 79 // The Subject content of the email is encoded in 80 myMail. subjectEncoding = Encoding. UTF8; 81 82 // email body 83 myMail. body = mailBody; 84 85 // email Body code 86 myMail. bodyEncoding = Encoding. default; 87 88 myMail. priority = Mai LPriority. High; 89 90 myMail. IsBodyHtml = isbodyHtml; 91 92 // Add an attachment 93 try 94 {95 if (attachmentsPath! = Null & attachmentsPath. length> 0) 96 {97 Attachment attachFile = null; 98 foreach (string path in attachmentsPath) 99{ 100 attachFile = new Attachment (path); 101 myMail. attachments. add (attachFile); 102} 103} 104} 105 catch (Exception err) 106 {107 throw new Exception ("error when adding attachment:" + err ); 108} 109 110 SmtpClient smtp = new SmtpClient (); 111 // specify the sender's email address and password to verify the sender's identity 112 smtp. credentials = new System. ne T. networkCredential (mailFrom, mailPwd); 113 smtp. useDefaultCredentials = true; 114 smtp. deliveryMethod = SmtpDeliveryMethod. network; 115 // set SMTP Mail Server 116 smtp. host = host; 117 // smtp. enableSsl = true; 118 // smtp. port = 587; 119 try120 {121 // send the email to SMTP Mail Server 122 smtp. send (myMail); 123 return true; 124 125} 126 catch (System. net. mail. smtpException ex) 127 {128 return false; 129} 130 131} 132} copy the code above as an aid for sending emails Class, the function is very complete, basically do not need to change the program: first write a console to test the program, the sending is successful, the results will not be able to, if written here is a blog, it's better for me to listen to a song. You may be interested in reading the following. copy code 1 Email email = new Email (); 2 email. mailFrom = "myEmail@163.com"; 3 email. mailPwd = "myPassword"; 4 email. mailSubject = "programmer's life"; 5 email. mailBody = "a very difficult task for many programmers"; 6 email. isbodyHtml = true; 7 email. host = "smtp.163.com"; 8 email. mailToArray = new string [] {"XX@xx.com", "XX@xx.cn"}; 9 email. mailCcArray = n Ew string [] {"XX@xx.com"}; 10 email. attachmentsPath = new string [] {}; 11 if (email. send () 12 {13 Console. writeLine ("email sent successfully ~ "); 14 Console. ReadKey (); 15} 16 else17 {18 Console. WriteLine (" email sending failed ~ "); 19 Console. ReadKey (); 20}

Related Article

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.