You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.3 KiB

  1. using Swift.API.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net.Mail;
  6. using System.Net.Mime;
  7. namespace Swift.web.Library
  8. {
  9. public class SmtpMailSetting
  10. {
  11. public SmtpMailSetting()
  12. {
  13. ToEmails = "";
  14. CcEmails = "";
  15. BccEmails = "";
  16. MsgSubject = "";
  17. MsgBody = "";
  18. SmtpPort = Convert.ToInt16(GetStatic.ReadWebConfig("SmtpPort", ""));
  19. SmtpServer = GetStatic.ReadWebConfig("SmtpServer", "");
  20. SendEmailId = GetStatic.ReadWebConfig("SendEmailId", "");
  21. SendEmailPwd = GetStatic.ReadWebConfig("SendEmailPwd", "");
  22. EnableSsl = true;
  23. }
  24. public int SmtpPort { get; set; }
  25. public string SmtpServer { get; set; }
  26. public string SendEmailId { get; set; }
  27. public string SendEmailPwd { get; set; }
  28. public string ToEmails { get; set; }
  29. public string CcEmails { get; set; }
  30. public string BccEmails { get; set; }
  31. public string MsgSubject { get; set; }
  32. public string MsgBody { get; set; }
  33. public bool EnableSsl { get; set; }
  34. public string Status { get; set; }
  35. /// <summary>
  36. /// Send mail through gme SMTP server
  37. /// </summary>
  38. /// <param name="smtpMail">
  39. /// receiver details mail body and subject
  40. /// </param>
  41. /// <returns>
  42. /// </returns>
  43. public string SendSmtpMail(SmtpMailSetting smtpMail, List<AttachmentModel> attachmentList = null, List<AttachmentModel> embadedList = null)
  44. {
  45. MailMessage mail = new MailMessage();
  46. SmtpClient SmtpServer = new SmtpClient();
  47. try
  48. {
  49. SmtpServer.Host = smtpMail.SmtpServer;
  50. SmtpServer.Port = smtpMail.SmtpPort;
  51. SmtpServer.UseDefaultCredentials = false;
  52. SmtpServer.Credentials = new System.Net.NetworkCredential(smtpMail.SendEmailId, smtpMail.SendEmailPwd);
  53. SmtpServer.EnableSsl = EnableSsl;
  54. mail.From = new MailAddress(smtpMail.SendEmailId);
  55. mail.To.Add(smtpMail.ToEmails);
  56. var ccList = smtpMail.CcEmails.Split(';');
  57. if (ccList[0] != "")
  58. {
  59. for (int i = 0; i < ccList.Length; i++)
  60. {
  61. mail.CC.Add(ccList[i]);
  62. }
  63. }
  64. //if (!string.IsNullOrEmpty(smtpMail.CcEmails))
  65. // mail.CC.Add(smtpMail.CcEmails);
  66. if (!string.IsNullOrEmpty(smtpMail.BccEmails))
  67. mail.Bcc.Add(smtpMail.BccEmails);
  68. mail.Subject = smtpMail.MsgSubject;
  69. mail.IsBodyHtml = true;
  70. mail.Body = smtpMail.MsgBody;
  71. if (attachmentList != null)
  72. {
  73. foreach (var item in attachmentList)
  74. {
  75. Attachment attachment = new Attachment(item.FilePath, item.FileType);
  76. ContentDisposition disposition = attachment.ContentDisposition;
  77. disposition.CreationDate = File.GetCreationTime(item.FilePath);
  78. disposition.ModificationDate = File.GetLastWriteTime(item.FilePath);
  79. disposition.ReadDate = File.GetLastAccessTime(item.FilePath);
  80. disposition.FileName = Path.GetFileName(item.FileName);
  81. disposition.Size = new FileInfo(item.FilePath).Length;
  82. disposition.DispositionType = DispositionTypeNames.Attachment;
  83. mail.Attachments.Add(attachment);
  84. }
  85. }
  86. if (embadedList != null)
  87. {
  88. if (embadedList.Count > 0)
  89. {
  90. AlternateView vw = GetEmbeddedImage(embadedList, smtpMail.MsgBody);
  91. mail.AlternateViews.Add(vw);
  92. }
  93. }
  94. System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
  95. SmtpServer.Send(mail);
  96. smtpMail.Status = "Y";
  97. SmtpServer.Dispose();
  98. }
  99. catch (SmtpFailedRecipientsException ex)
  100. {
  101. for (int i = 0; i < ex.InnerExceptions.Length; i++)
  102. {
  103. SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
  104. if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable)
  105. {
  106. // Console.WriteLine("Delivery failed - retrying in 5 seconds.");
  107. System.Threading.Thread.Sleep(5000);
  108. SmtpServer.Send(mail);
  109. }
  110. else
  111. {
  112. // Console.WriteLine("Failed to deliver message to {0}", ex.InnerExceptions[i].FailedRecipient);
  113. //throw ex;
  114. smtpMail.Status = "N";
  115. smtpMail.MsgBody = ex.Message;
  116. GetStatic.EmailNotificationLog(smtpMail);
  117. }
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. smtpMail.Status = "N";
  123. smtpMail.MsgBody = ex.ToString();
  124. GetStatic.EmailNotificationLog(smtpMail);
  125. }
  126. finally
  127. {
  128. SmtpServer.Dispose();
  129. }
  130. GetStatic.EmailNotificationLog(smtpMail);
  131. return (smtpMail.Status == "Y") ? "Mail Send" : "Error while sending Email";
  132. }
  133. private AlternateView GetEmbeddedImage(List<AttachmentModel> attachmentList, string htmlBody)
  134. {
  135. AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
  136. foreach (var item in attachmentList)
  137. {
  138. byte[] bytes = System.Convert.FromBase64String(item.Byte64String);
  139. LinkedResource res = new LinkedResource(new MemoryStream(bytes), item.FileType);
  140. res.ContentId = item.FileName;
  141. alternateView.LinkedResources.Add(res);
  142. }
  143. return alternateView;
  144. }
  145. }
  146. }