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
158 lines
6.3 KiB
using Swift.API.Common;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net.Mail;
|
|
using System.Net.Mime;
|
|
|
|
namespace Swift.web.Library
|
|
{
|
|
public class SmtpMailSetting
|
|
{
|
|
public SmtpMailSetting()
|
|
{
|
|
ToEmails = "";
|
|
CcEmails = "";
|
|
BccEmails = "";
|
|
MsgSubject = "";
|
|
MsgBody = "";
|
|
SmtpPort = Convert.ToInt16(GetStatic.ReadWebConfig("SmtpPort", ""));
|
|
SmtpServer = GetStatic.ReadWebConfig("SmtpServer", "");
|
|
SendEmailId = GetStatic.ReadWebConfig("SendEmailId", "");
|
|
SendEmailPwd = GetStatic.ReadWebConfig("SendEmailPwd", "");
|
|
EnableSsl = true;
|
|
}
|
|
|
|
public int SmtpPort { get; set; }
|
|
public string SmtpServer { get; set; }
|
|
public string SendEmailId { get; set; }
|
|
public string SendEmailPwd { get; set; }
|
|
public string ToEmails { get; set; }
|
|
public string CcEmails { get; set; }
|
|
public string BccEmails { get; set; }
|
|
public string MsgSubject { get; set; }
|
|
public string MsgBody { get; set; }
|
|
public bool EnableSsl { get; set; }
|
|
public string Status { get; set; }
|
|
|
|
/// <summary>
|
|
/// Send mail through gme SMTP server
|
|
/// </summary>
|
|
/// <param name="smtpMail">
|
|
/// receiver details mail body and subject
|
|
/// </param>
|
|
/// <returns>
|
|
/// </returns>
|
|
public string SendSmtpMail(SmtpMailSetting smtpMail, List<AttachmentModel> attachmentList = null, List<AttachmentModel> embadedList = null)
|
|
{
|
|
MailMessage mail = new MailMessage();
|
|
SmtpClient SmtpServer = new SmtpClient();
|
|
try
|
|
{
|
|
SmtpServer.Host = smtpMail.SmtpServer;
|
|
SmtpServer.Port = smtpMail.SmtpPort;
|
|
SmtpServer.UseDefaultCredentials = false;
|
|
SmtpServer.Credentials = new System.Net.NetworkCredential(smtpMail.SendEmailId, smtpMail.SendEmailPwd);
|
|
SmtpServer.EnableSsl = EnableSsl;
|
|
|
|
mail.From = new MailAddress(smtpMail.SendEmailId);
|
|
mail.To.Add(smtpMail.ToEmails);
|
|
var ccList = smtpMail.CcEmails.Split(';');
|
|
if (ccList[0] != "")
|
|
{
|
|
for (int i = 0; i < ccList.Length; i++)
|
|
{
|
|
mail.CC.Add(ccList[i]);
|
|
}
|
|
}
|
|
|
|
//if (!string.IsNullOrEmpty(smtpMail.CcEmails))
|
|
// mail.CC.Add(smtpMail.CcEmails);
|
|
if (!string.IsNullOrEmpty(smtpMail.BccEmails))
|
|
mail.Bcc.Add(smtpMail.BccEmails);
|
|
|
|
mail.Subject = smtpMail.MsgSubject;
|
|
mail.IsBodyHtml = true;
|
|
mail.Body = smtpMail.MsgBody;
|
|
|
|
if (attachmentList != null)
|
|
{
|
|
foreach (var item in attachmentList)
|
|
{
|
|
Attachment attachment = new Attachment(item.FilePath, item.FileType);
|
|
ContentDisposition disposition = attachment.ContentDisposition;
|
|
disposition.CreationDate = File.GetCreationTime(item.FilePath);
|
|
disposition.ModificationDate = File.GetLastWriteTime(item.FilePath);
|
|
disposition.ReadDate = File.GetLastAccessTime(item.FilePath);
|
|
disposition.FileName = Path.GetFileName(item.FileName);
|
|
disposition.Size = new FileInfo(item.FilePath).Length;
|
|
disposition.DispositionType = DispositionTypeNames.Attachment;
|
|
mail.Attachments.Add(attachment);
|
|
}
|
|
}
|
|
|
|
if (embadedList != null)
|
|
{
|
|
if (embadedList.Count > 0)
|
|
{
|
|
AlternateView vw = GetEmbeddedImage(embadedList, smtpMail.MsgBody);
|
|
mail.AlternateViews.Add(vw);
|
|
}
|
|
}
|
|
|
|
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
|
|
SmtpServer.Send(mail);
|
|
smtpMail.Status = "Y";
|
|
SmtpServer.Dispose();
|
|
}
|
|
catch (SmtpFailedRecipientsException ex)
|
|
{
|
|
for (int i = 0; i < ex.InnerExceptions.Length; i++)
|
|
{
|
|
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
|
|
if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable)
|
|
{
|
|
// Console.WriteLine("Delivery failed - retrying in 5 seconds.");
|
|
System.Threading.Thread.Sleep(5000);
|
|
SmtpServer.Send(mail);
|
|
}
|
|
else
|
|
{
|
|
// Console.WriteLine("Failed to deliver message to {0}", ex.InnerExceptions[i].FailedRecipient);
|
|
//throw ex;
|
|
smtpMail.Status = "N";
|
|
smtpMail.MsgBody = ex.Message;
|
|
GetStatic.EmailNotificationLog(smtpMail);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
smtpMail.Status = "N";
|
|
smtpMail.MsgBody = ex.ToString();
|
|
GetStatic.EmailNotificationLog(smtpMail);
|
|
}
|
|
finally
|
|
{
|
|
SmtpServer.Dispose();
|
|
}
|
|
GetStatic.EmailNotificationLog(smtpMail);
|
|
|
|
return (smtpMail.Status == "Y") ? "Mail Send" : "Error while sending Email";
|
|
}
|
|
|
|
private AlternateView GetEmbeddedImage(List<AttachmentModel> attachmentList, string htmlBody)
|
|
{
|
|
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
|
|
foreach (var item in attachmentList)
|
|
{
|
|
byte[] bytes = System.Convert.FromBase64String(item.Byte64String);
|
|
LinkedResource res = new LinkedResource(new MemoryStream(bytes), item.FileType);
|
|
res.ContentId = item.FileName;
|
|
alternateView.LinkedResources.Add(res);
|
|
}
|
|
return alternateView;
|
|
}
|
|
}
|
|
}
|
|
|