void updateDetails(mailDetails mail, bool Cancelled, Exception Error) { try { string error = null; var updateData = mail.mail; int int_failed_count = 0; if (updateData.int_failed_count != null) { int_failed_count = updateData.int_failed_count.Value; } int int_satues = 0; if (!Cancelled && Error == null) { updateData.dt_send_date = DateTime.Now; int_satues = 255; } else { int_satues = -1; logHelper.LogException(Error, main_path); if (Error != null) { if (Error.InnerException != null) { error = Error.InnerException.Message; } else { error = Error.Message; } } int_failed_count++; } updateData.str_error = error; updateData.int_status = int_satues; updateData.int_failed_count = int_failed_count; dbHelper helper = new dbHelper(); if (helper.UpdateData(updateData)) { collection.Remove(mail); } } catch (Exception er) { logHelper.LogException(er, main_path); } }
private void WorkerThreadFunc() { if (settings != null && true) { dbHelper helper = new dbHelper(); string smtpEmailAddress = settings["SMTPUserName"]; string smtpPassword = settings["SMTPPassword"]; // string fromEmail = settings["FromEmail"]; // string fromDisplayName = settings["FromDisplayName"]; string smtpClientHost = settings["SMTPHostName"]; int smtoClientPort = int.Parse(settings["SMTPHostPort"]); bool enableSsl = bool.Parse(settings["EnableSSL"]); bool useDefaultCredentials = bool.Parse(settings["UseDefaultCredentials"]); int emailForBulk = 50, reTryTime = 5; int.TryParse(settings["EmailForBulk"], out emailForBulk); int.TryParse(settings["ReTryTime"], out reTryTime); var databundle = helper.GetmailBulk(DateTime.Now, emailForBulk); var notsendbundle = helper.GetFailedBulk(reTryTime); if (databundle != null) { databundle.AddRange(notsendbundle); } else { databundle = notsendbundle; } if (databundle != null) { helper.SetSettingFromKey("LastRunTime", DateTime.Now.ToString()); Parallel.ForEach(databundle, item => { string newGuid = Guid.NewGuid().ToString(); var mail = new mailDetails() { guid = newGuid, mail = item }; collection.Add(mail); sendEmail(mail, smtpEmailAddress, smtpPassword, smtpClientHost, smtoClientPort, enableSsl, useDefaultCredentials); }); } } current_thread_count--; if (!emailerTimer.Enabled) { emailerTimer.Start(); } }
private void WorkerThreadFunc() { if (settings != null && true) { dbHelper helper = new dbHelper(); string smtpEmailAddress = settings["SMTPUserName"]; string smtpPassword = settings["SMTPPassword"]; // string fromEmail = settings["FromEmail"]; // string fromDisplayName = settings["FromDisplayName"]; string smtpClientHost = settings["SMTPHostName"]; int smtoClientPort = int.Parse(settings["SMTPHostPort"]); bool enableSsl = bool.Parse(settings["EnableSSL"]); bool useDefaultCredentials = bool.Parse(settings["UseDefaultCredentials"]); int emailForBulk = 50, reTryTime = 5; int.TryParse(settings["EmailForBulk"], out emailForBulk); int.TryParse(settings["ReTryTime"], out reTryTime); var databundle = helper.GetmailBulk(DateTime.Now, emailForBulk); var notsendbundle = helper.GetFailedBulk(reTryTime); if (databundle != null) { databundle.AddRange(notsendbundle); } else databundle = notsendbundle; if (databundle != null) { helper.SetSettingFromKey("LastRunTime", DateTime.Now.ToString()); Parallel.ForEach(databundle, item => { string newGuid = Guid.NewGuid().ToString(); var mail = new mailDetails() { guid = newGuid, mail = item }; collection.Add(mail); sendEmail(mail, smtpEmailAddress, smtpPassword, smtpClientHost, smtoClientPort, enableSsl, useDefaultCredentials); }); } } current_thread_count--; if (!emailerTimer.Enabled) emailerTimer.Start(); }
void updateDetails(mailDetails mail, bool Cancelled, Exception Error) { try { string error = null; var updateData = mail.mail; int int_failed_count = 0; if (updateData.int_failed_count != null) int_failed_count = updateData.int_failed_count.Value; int int_satues = 0; if (!Cancelled && Error == null) { updateData.dt_send_date = DateTime.Now; int_satues = 255; } else { int_satues = -1; logHelper.LogException(Error, main_path); if (Error != null) { if (Error.InnerException != null) error = Error.InnerException.Message; else error = Error.Message; } int_failed_count++; } updateData.str_error = error; updateData.int_status = int_satues; updateData.int_failed_count = int_failed_count; dbHelper helper = new dbHelper(); if (helper.UpdateData(updateData)) collection.Remove(mail); } catch (Exception er) { logHelper.LogException(er, main_path); } }
private void sendEmail(mailDetails mail, string smtpEmailAddress, string smtpPassword, string smtpClientHost, int smtoClientPort, bool enableSsl, bool useDefaultCredentials) { string guid = mail.guid; try { NetworkCredential cred = new NetworkCredential(smtpEmailAddress, smtpPassword); MailMessage msg = new MailMessage(); foreach (var email in mail.mail.tbl_email_to_address) { if (email.int_type == 1) { msg.To.Add(new MailAddress(email.str_email_address, email.str_email_name)); } else if (email.int_type == 2) { msg.CC.Add(new MailAddress(email.str_email_address, email.str_email_name)); } else if (email.int_type == 3) { msg.Bcc.Add(new MailAddress(email.str_email_address, email.str_email_name)); } } msg.Subject = mail.mail.tbl_email_awaiting_details.str_subject; msg.IsBodyHtml = true; msg.Body = mail.mail.tbl_email_awaiting_details.str_body; msg.From = new MailAddress(mail.mail.tbl_email_awaiting_details.str_from_address, mail.mail.tbl_email_awaiting_details.str_from_name); foreach (var attac in mail.mail.tbl_email_awaiting_details.tbl_email_attachment) { var attachmentFilename = attac.str_file_path; if (File.Exists(attachmentFilename)) { Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet); ContentDisposition disposition = attachment.ContentDisposition; disposition.CreationDate = File.GetCreationTime(attachmentFilename); disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename); disposition.ReadDate = File.GetLastAccessTime(attachmentFilename); disposition.FileName = Path.GetFileName(attachmentFilename); disposition.Size = new FileInfo(attachmentFilename).Length; disposition.DispositionType = DispositionTypeNames.Attachment; msg.Attachments.Add(attachment); attac.bit_attached = true; } else { attac.bit_attached = false; attac.str_error = "File not found."; } } SmtpClient client = new SmtpClient(smtpClientHost, smtoClientPort); client.SendCompleted += (s, e) => { client_SendCompleted(s, e); client.Dispose(); msg.Dispose(); }; ; client.Credentials = cred; client.UseDefaultCredentials = useDefaultCredentials; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = enableSsl; client.SendAsync(msg, guid); } catch (Exception e) { updateDetails(mail, true, e); } }
private void sendEmail(mailDetails mail, string smtpEmailAddress, string smtpPassword, string smtpClientHost, int smtoClientPort, bool enableSsl, bool useDefaultCredentials) { string guid = mail.guid; try { NetworkCredential cred = new NetworkCredential(smtpEmailAddress, smtpPassword); MailMessage msg = new MailMessage(); foreach (var email in mail.mail.tbl_email_to_address) { if (email.int_type == 1) { msg.To.Add(new MailAddress(email.str_email_address, email.str_email_name)); } else if (email.int_type == 2) { msg.CC.Add(new MailAddress(email.str_email_address, email.str_email_name)); } else if (email.int_type == 3) { msg.Bcc.Add(new MailAddress(email.str_email_address, email.str_email_name)); } } msg.Subject = mail.mail.tbl_email_awaiting_details.str_subject; msg.IsBodyHtml = true; msg.Body = mail.mail.tbl_email_awaiting_details.str_body; msg.From = new MailAddress(mail.mail.tbl_email_awaiting_details.str_from_address, mail.mail.tbl_email_awaiting_details.str_from_name); foreach (var attac in mail.mail.tbl_email_awaiting_details.tbl_email_attachment) { var attachmentFilename = attac.str_file_path; if (File.Exists(attachmentFilename)) { Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet); ContentDisposition disposition = attachment.ContentDisposition; disposition.CreationDate = File.GetCreationTime(attachmentFilename); disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename); disposition.ReadDate = File.GetLastAccessTime(attachmentFilename); disposition.FileName = Path.GetFileName(attachmentFilename); disposition.Size = new FileInfo(attachmentFilename).Length; disposition.DispositionType = DispositionTypeNames.Attachment; msg.Attachments.Add(attachment); attac.bit_attached = true; } else { attac.bit_attached = false; attac.str_error = "File not found."; } } SmtpClient client = new SmtpClient(smtpClientHost, smtoClientPort); client.SendCompleted += (s, e) => { client_SendCompleted(s, e); client.Dispose(); msg.Dispose(); };; client.Credentials = cred; client.UseDefaultCredentials = useDefaultCredentials; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.EnableSsl = enableSsl; client.SendAsync(msg, guid); } catch (Exception e) { updateDetails(mail, true, e); } }