Before I have never done the ability to send the mailbox, the previous period of time the project needs, after looking for a lot of posts, finally realized.
Then there was a tidy up and wrote a class. Pass the information directly to the class and you can send it.
Here also need to explain is, send mailbox need to open POP3/SMTP service, otherwise QQ mailbox, NetEase mailbox and so will error. Receive the mailbox does not have to open, the method of opening Baidu will know.
public static class Emailhelper {//<summary>///e-mail//</summary>/<param name= "Subje CT "> Mail subject </param>//<param name=" MSG "> Message contents </param>//<param name=" FilePath "> Attachment address if not added Attachment null or "" </param>//<param name= "Senderemail" > Sender email Address </param>//<param name= "Senderpwd" Sender Email Password </param>//<param name= "Recipientemail" > Recipient mailbox </param> public static void SendMail (string Subject, String msg, String FilePath, String senderemail, String senderpwd, params string[] recipientemail) {if (! Checkisnotemptyornull (Subject, MSG, Senderemail, senderpwd) | | Recipientemail = = NULL | | Recipientemail.length = = 0) {throw new Exception ("Invalid input information"); } try {string[] Sendfromuser = senderemail.split (' @ '); Constructs an email message object MailMessage message = new MailMessage (); Determine the SMTP server address. Instantiate an SMTP client System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient ("SMTP." + sendfromuser[1]); Constructs the sender Address object message. from = new MailAddress (Senderemail, sendfromuser[0], Encoding.UTF8); Constructs a recipient Address object, foreach (String userName in Recipientemail) {message. To.add (New MailAddress (UserName, Username.split (' @ ') [0], Encoding.UTF8)); } if (!string. IsNullOrEmpty (FilePath)) {Attachment attach = new Attachment (FilePath); Get the file information contentdisposition disposition = attach. Contentdisposition; Disposition. CreationDate = System.IO.File.GetCreationTime (FilePath); Disposition. Modificationdate = System.IO.File.GetLastWriteTime (FilePath); Disposition. Readdate = System.IO.File.GetLastAccessTime (FilePath); Add an attachment message to the message. Attachments.Add (attach); }//Add message subject and content message. Subject = Subject; Message. subjectencoding = Encoding.UTF8; Message. Body = msg; Message. Bodyencoding = Encoding.UTF8; Sets the message client. Deliverymethod = Smtpdeliverymethod.network; Message. bodyencoding = System.Text.Encoding.UTF8; Message. Isbodyhtml = false; If the server supports a secure connection, set the secure connection to true. GMAIL,QQ support, 163 does not support switch (Sendfromuser[1]) {case ' gmail.com ': Case ' qq.com ': Client. Enablessl = true; Break Default:client. Enablessl = false; Break }//Set user name and password. Client. useDefaultCredentials = false; User login information NetworkCredential mycredentials = new NetworkCredential (Senderemail, senderpwd); Client. Credentials = mycredentials; Send mail client. Send (message); } catch (Exception ex) {throw (ex); }}///<summary>///Verify that all incoming strings cannot be empty or null////</summary>//<param name= "PS" > Parameter list </pa Ram>//<returns> Neither is null or NULL returns TRUE, otherwise returns FALSE</RETURNS> public static bool Checkisnotemptyornull (params string[] PS) {if (PS! = null) {foreach (String item In PS) {if (string. IsNullOrEmpty (item)) return false; } return true; } return false; } }
Call the method directly, pass the information that needs to send, can send the mailbox.
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.