How to implement e-mail in C #

Source: Internet
Author: User
Tags mailmessage smtpclient
This article is mainly for everyone to introduce in detail the C # Implementation of the method of sending mail, with a certain reference value, interested in small partners can refer to

The example of this article for you to share the Android nine Gongge picture display of the specific code for your reference, the specific content is as follows


#region Send mail section private static String Frommail = "1111@126.com";     Mailbox name private static String mailpwd = "111111";  Password private static string tomail = "2222@163.com"; Mailbox server private static string filestr;//current picture path, when adding attachments with///<summary>/////</summary>/ <param name= "FILEURL" > Accessory address to ~ </param>//<param name= "screen" > whether to send screenshots </param>//<re turns></returns> public static string SendMail (string fileUrl, String screens) {MailAddress from      = new MailAddress (frommail);      MailAddress to = new MailAddress (tomail);      MailMessage message = new MailMessage (from, to); Message. Subject = "M mail" +11111;//subject message.      subjectencoding = System.Text.Encoding.UTF8;      Attachment attachfile = new Attachment (FILESTR); if (screen = = "True") message.      Attachments.Add (Attachfile);      string[] files = fileurl.split (' ~ '); for (int i = 0; i < files. Length;  i++) {      if (file.exists (files[i))) {Attachment attachFile1 = new Attachment (FILEURL); Message.        Attachments.Add (ATTACHFILE1); }} try {SmtpClient client = new SmtpClient ("SMTP." + from.        Host);               SendMail (client, from, Mailpwd, to, message); Return "Send mail success! Includes attachment: "+ FileUrl +" included?      "+ screen +" "+ DateTime.Now.ToString (); } catch (Smtpexception ex) {//If the error is not found for the server, try not to add SMTP. prefix server if (ex. StatusCode = = smtpstatuscode.generalfailure) {try {//some mail servers do not add SMTP. prefix Smt Pclient client = new SmtpClient (from.            Host);                      SendMail (client, from, Mailpwd, to, message); Return "Send mail success! Includes attachment: "+ FileUrl +" included?          "+ screen +" "+ DateTime.Now.ToString (); ' Catch (smtpexception err) {return ' failed to send mail! "+ Err.          Message + "" + DateTime.Now.ToString ();     }} else   {return ' failed to send mail! "+ Ex.        Message + "" + DateTime.Now.ToString ();       }}}//Send mail according to specified parameters private static void SendMail (SmtpClient client, mailaddress from, string password, MailAddress to, MailMessage message) {//Do not use default credentials, note that this sentence must be placed in the client. Credentials the upper client.      useDefaultCredentials = false; Specify the user name, password client. Credentials = new NetworkCredential (from.      Address, password); Messages are sent over the network to the server client.      Deliverymethod = Smtpdeliverymethod.network; try {client.      Send (message);      } catch {throw; finally {//releases the consumed resource message in a timely manner.      Dispose (); }} #endregion
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.