public void SendArchive(MailSendArchive mail, long id) { using (con = new NpgsqlConnection(this.ConnectionString)) { con.Open(); con.Execute("DELETE FROM mailsend WHERE id = " + id); con.InsertPg <MailSendArchive>(mail); } }
public void StartEngine() { // take mail records to send List <MailSend> mailList = GetMailsFromDB(); if (mailList.Count > 0) { foreach (MailSend mail in mailList) { MailSendArchive msa = new MailSendArchive(); msa.Body = mail.Body; msa.FilePath = mail.FilePath; msa.Description = mail.Description; msa.InsertDate = DateTime.Now; msa.MailType = mail.MailType; msa.Module = mail.Module; msa.Sender = mail.Sender; msa.Subject = mail.Subject; msa.ToAddress = mail.ToAddress; msa.CC = mail.CC; MailProfile mprof = GetMailProfile(mail.Sender); if (mprof == null) { msa.ErrDescription = "Mail Profile doesn't exist!"; msa.MailStatus = 2; msa.SendDate = null; } else { try { if (mprof.SendMethod == 1) { //amazon List <string> adr = new List <string>(); List <string> ccadr = new List <string>(); if (mail.ToAddress.Contains(';')) { adr = mail.ToAddress.Split(';').ToList <string>(); } else { adr.Add(mail.ToAddress); } if (mail.CC != null) { if (mail.CC != "") { if (mail.CC.Contains(';')) { ccadr = mail.CC.Split(';').ToList <string>(); } else { ccadr.Add(mail.CC); } } } SendAmazonEmails(mail.Body, mail.Subject, adr, ccadr, mail.FilePath, mprof.DisplayName, mprof.Sender); } else { //send method smtp MailMessage mm = new MailMessage(); SmtpClient smtpServer = new SmtpClient(mprof.Server); mm.From = new MailAddress(mprof.Sender, mprof.DisplayName); if (mail.ToAddress.Contains(';')) { List <string> adr = mail.ToAddress.Split(';').ToList <string>(); foreach (string item in adr) { mm.To.Add(item); } } else { mm.To.Add(mail.ToAddress); } if (mail.CC != null) { if (mail.CC != "") { if (mail.CC.Contains(';')) { List <string> adr = mail.CC.Split(';').ToList <string>(); foreach (string item in adr) { mm.CC.Add(item); } } else { mm.CC.Add(mail.CC); } } } System.Net.Mail.Attachment attachment; if (mail.FilePath != "" && mail.FilePath != null) { attachment = new System.Net.Mail.Attachment(mail.FilePath); mm.Attachments.Add(attachment); } if (mprof.Port == null) { smtpServer.Port = 25; } else { smtpServer.Port = Convert.ToInt32(mprof.Port); } smtpServer.Credentials = new System.Net.NetworkCredential(mprof.Sender, mprof.Password); smtpServer.Send(mm); } } catch (Exception ex) { msa.MailStatus = 2; msa.SendDate = null; msa.ErrDescription = ex.Message; Console.WriteLine(ex.Message + ex.StackTrace); } } SendArchive(msa, mail.Id); } } }