1 class SmtpEmail 2 { 3 SmtpClient smtpclient; 4 MailMessage msg; 5 Attachment attachment; 6 public void sendMail(String form, String[] to, String subject, String body, String attachmentpath) 7 { 8 if (to == null || to.Length <= 0) { 9 return; 10 } 11 try 12 { 13 smtpclient = new SmtpClient("smtp.URL"); 14 smtpclient.UseDefaultCredentials = true; 15 smtpclient.Credentials = new NetworkCredential("username", "*****"); 16 smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network; 17 msg = new MailMessage(); 18 19 20 for (int i = 0; i < to.Length; i++) 21 { 22 msg.To.Add(to[i]); 23 } 24 if (msg.To.Count <= 0) { 25 return; 26 } 27 msg.From = form; 28 msg.Subject = subject; 29 msg.Body = body; 30 if (attachment!=null && !attachmentpath.Length.Equals(0)) 31 { 32 this.attachment = new Attachment(attachmentpath); 33 msg.Attachments.Add(attachment); 34 } 35 msg.BodyEncoding = Encoding.UTF8; 36 msg.IsBodyHtml = true; 37 smtpclient.Send(msg); 38 } 39 catch (Exception err) 40 { 41 MessageBox.Show(err.Message); 42 return; 43 } 44 } 45 }