It is very simple to realize the function of sending emails in C#. Microsoft has already done most of the things for us. Not much to say, the code:
1 public class Email 2 { 3 /// <summary> 4 /// Sender 5 /// </summary> 6 public string mailFrom { get ; set ; } 7 8 /// <summary> 9 /// Receive Person 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 /// 标题 20 /// </summary> 21 public string mailSubject { get; set; } 22 23 /// <summary> 24 /// 正文 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 text is in html format 40 /// </summary> 41 public boolisbodyHtml { get ; set ; } 42 43 /// <summary> 44 /// attachments 45 /// </summary> 46 public string [] attachmentsPath { get ; set ; } 47 48 public bool Send() 49 { 50 // Initialize the MailAddress instance with the specified email address 51 MailAddress maddr = new MailAddress(mailFrom); 52 // Initialize the MailMessage instance 53 MailMessage myMail = new MailMessage(); 54 55 56 // Add mail address to recipient address collection 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 email address to CC recipient address collection 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 // The subject of the email is 77 myMail.Subject = mailSubject; 78 79 // The encoding used for the subject of the email is 80 myMail.SubjectEncoding = Encoding.UTF8; 81 82 // email body 83 myMail.Body = mailBody; 84 85 // encoding of the email body 86 myMail.BodyEncoding = Encoding.Default; 87 88 myMail.Priority = MailPriority.High; 89 90 myMail.IsBodyHtml = isbodyHtml; 91 92 // Add attachments if there are attachments 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 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.Net.NetworkCredential(mailFrom, mailPwd); 113 114 115 // Set SMTP mail server 116 smtp.Host = host; 117 smtp.EnableSsl = true ; 118 try 119 { 120 // Send mail to SMTP mail server 121 smtp.Send(myMail); 122 return true ; 123 124 } 125 catch (System.Net.Mail.SmtpException) 126 { 127 return false ; 128 } 129 130 } 131 }
With the above class, just call Send to access it directly
1 Email email = new Email(); 2 email.mailFrom = account number; 3 email.mailPwd = password; 4 email.mailSubject = titile; // title 5 email.mailBody = content; // body 6 email.isbodyHtml = true ; // Whether it is HTML 7 email.host = mailbox host; // If it is QQ mailbox: smtp:qq.com, and so on 8 email.mailToArray = new string [] { emailaddress }; // Receiver mail collection 9 email .mailCcArray = new string[] { }; // CC email collection 10 email.Send();
At this point, the mail sending function is completed, let's try it now