C # Send email,

Source: Internet
Author: User
Tags mailmessage smtpclient

C # Send email,

I have never used the email sending function before. Some time ago, the project needs to be able to send emails after I have found many posts.

Then I sorted it out and wrote a class. You can send the message directly to the class.

It should also be noted that the POP3/SMTP service must be activated for sending mail; otherwise, an error will be reported for QQ mail and Netease mail. You don't need to activate the email address you receive. You can use Baidu to activate the email address.

1 public static class EmailHelper 2 {3 /// <summary> 4 // send email 5 /// </summary> 6 /// <param name = "subject"> email Subject </param> 7 /// <param name = "msg"> email content </param> 8 /// <param name = "filePath"> attachment address, if null or "</param> 9 // <param name =" senderEmail "> sender's email address </param> 10 /// <param name = "senderPwd"> sender's email password </param> 11 // <param name = "recipientEmail"> recipient's email </param> 12 public static void SendMail (string Subject, string msg, string filePath, string senderEmail, string senderPwd, params string [] recipientEmail) 13 {14 if (! CheckIsNotEmptyOrNull (subject, msg, senderEmail, senderPwd) | recipientEmail = null | recipientEmail. length = 0) 15 {16 throw new Exception ("invalid input information"); 17} 18 try 19 {20 string [] sendFromUser = senderEmail. split ('@'); 21 22 // construct an Email Message object 23 MailMessage message = new MailMessage (); 24 25 // determine the smtp server address. Instantiate an Smtp client 26 System. net. mail. smtpClient client = new System. net. mail. smtpClient ("smtp. "+ sendFromUser [1]); 27 28 // construct the sender address object 29 message. from = new MailAddress (senderEmail, sendFromUser [0], Encoding. UTF8); 30 31 // construct the recipient address object 32 foreach (string userName in recipientEmail) 33 {34 message. to. add (new MailAddress (userName, userName. split ('@') [0], Encoding. UTF8); 35} 36 37 if (! String. isNullOrEmpty (filePath) 38 {39 Attachment attach = new Attachment (filePath); 40 // obtain the file information 41 ContentDisposition disposition = attach. contentDisposition; 42 disposition. creationDate = System. IO. file. getCreationTime (filePath); 43 disposition. modificationDate = System. IO. file. getLastWriteTime (filePath); 44 disposition. readDate = System. IO. file. getLastAccessTime (filePath); 45 // Add the attachment 46 mess to the email Age. attachments. add (attach); 47} 48 49 // Add email subject and content 50 message. subject = subject; 51 message. subjectEncoding = Encoding. UTF8; 52 message. body = msg; 53 message. bodyEncoding = Encoding. UTF8; 54 55 // set Mail Information 56 client. deliveryMethod = SmtpDeliveryMethod. network; 57 message. bodyEncoding = System. text. encoding. UTF8; 58 message. isBodyHtml = false; 59 60 // if the server supports secure connections, set the Security connection to true. 61 // gmail, qq supported, 163 does not support 62 switch (sendFromUser [1]) 63 {64 case "gmail.com": 65 case "qq.com": 66 client. enableSsl = true; 67 break; 68 default: 69 client. enableSsl = false; 70 break; 71} 72 73 // set the user name and password. 74 client. usedefacrecredentials = false; 75 // user logon information 76 NetworkCredential myCredentials = new NetworkCredential (senderEmail, senderPwd); 77 client. credentials = myCredentials; 78 // send mail 79 client. send (message); 80} 81 catch (Exception ex) 82 {83 throw (ex ); 84} 85} 86 87 // <summary> 88 // verify that all input strings cannot be empty or null 89 // </summary> 90 // <param name = "ps"> parameter list </param> 91 // <returns> are not empty or null. tr is returned. Ue, otherwise false is returned </returns> 92 public static bool CheckIsNotEmptyOrNull (params string [] ps) 93 {94 if (ps! = Null) 95 {96 foreach (string item in ps) 97 {98 if (string. isNullOrEmpty (item) return false; 99} 100 return true; 101} 102 return false; 103} 104}

 

, Directly call the method, pass the information to be sent, you can send the mailbox.

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.