Guest User

Untitled

a guest
Aug 25th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. Send mail using SMTP server
  2. public bool SendMail(string mailFrom, string mailTo, string mailCC, string mailBCC, string subject, string body, string attachment, bool isBodyHtml)
  3. {
  4. bool SendStatus = false;
  5. System.Net.Mail.MailMessage mailMesg = new System.Net.Mail.MailMessage(mailFrom, mailTo);
  6.  
  7. if (mailCC != string.Empty)
  8. mailMesg.CC.Add(mailCC);
  9.  
  10. if (mailBCC != string.Empty)
  11. mailMesg.Bcc.Add(mailBCC);
  12.  
  13. if (!string.IsNullOrEmpty(attachment))
  14. {
  15. System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(attachment);
  16. mailMesg.Attachments.Add(attach);
  17. }
  18. mailMesg.Subject = subject;
  19. mailMesg.Body = body;
  20. mailMesg.IsBodyHtml = isBodyHtml;
  21. mailMesg.ReplyTo = new System.Net.Mail.MailAddress(mailFrom);
  22.  
  23. System.Net.Mail.SmtpClient objSMTP = new System.Net.Mail.SmtpClient();
  24.  
  25.  
  26. string Host = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString();
  27. string UserName = System.Configuration.ConfigurationManager.AppSettings["MailUserId"].ToString();
  28. string password = System.Configuration.ConfigurationManager.AppSettings["MailPassword"].ToString();
  29.  
  30.  
  31. objSMTP.Host = Host;
  32. objSMTP.Port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"].ToString());
  33. objSMTP.Credentials = new System.Net.NetworkCredential(UserName, password);
  34.  
  35.  
  36. objSMTP.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  37.  
  38. try
  39. {
  40. objSMTP.Send(mailMesg);
  41. SendStatus = true;
  42. }
  43. catch (Exception ex)
  44. {
  45. throw ex;
  46. }
  47. finally
  48. {
  49. mailMesg.Dispose();
  50. mailMesg = null;
  51. }
  52.  
  53.  
  54. return SendStatus;
  55.  
  56. }
Add Comment
Please, Sign In to add comment