Guest User

Untitled

a guest
Jan 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public string SendEmail(string subject, string body, string sender, string receiver, string filePath = null)
  2. {
  3.  
  4. SmtpClient client = new SmtpClient(Configuration["SmtpClient"]);
  5. client.Port = Convert.ToInt32(Configuration["SmtpPort"]); //587
  6. client.UseDefaultCredentials = Convert.ToInt32(Configuration["UseDefaultCredential"]) == 1 ? true : false;
  7. if (!string.IsNullOrEmpty(Configuration["SmtpUsername"]))
  8. client.Credentials = new NetworkCredential(Configuration["SmtpUsername"].Trim(), Configuration["SmtpPassword"].Trim());
  9. client.EnableSsl = Convert.ToInt32(Configuration["SmtpEnableSSL"]) == 1 ? true : false;
  10. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  11.  
  12. MailMessage mailMessage = new MailMessage();
  13. mailMessage.From = new MailAddress(sender);
  14. mailMessage.To.Add(receiver);
  15. mailMessage.IsBodyHtml = true;
  16. mailMessage.Subject = subject;
  17.  
  18. if (!string.IsNullOrEmpty(filePath))
  19. {
  20. mailMessage.AlternateViews.Add(getEmbeddedImage(filePath, body));
  21. Attachment attachments = new Attachment(filePath);
  22. //attachments.ContentDisposition.Inline = true;
  23. mailMessage.Attachments.Add(attachments);
  24. }
  25. else
  26. {
  27. mailMessage.Body = body;
  28. }
  29. client.Send(mailMessage);
  30.  
  31. return "OK";
  32. }
Add Comment
Please, Sign In to add comment