private void SendEmailToAdmin(string fullErrorMessage) { var emailToText = ConfigurationManager.AppSettings["EmailAlertSystemError"]; var emailTo = emailToText.Split(',').ToList(); List <MailProfile> mailTo = new List <MailProfile>(); for (int i = 0; i < emailTo.Count(); i++) { MailProfile mail = new MailProfile() { MailAddress = emailTo.ElementAt(i) }; mailTo.Add(mail); } SendMailMessage message = new SendMailMessage(); message.emailFrom = ConfigurationManager.AppSettings["From"]; message.emailFromDisplayName = ConfigurationManager.AppSettings["FromName"]; message.emailTo = mailTo; message.emailSubject = "Subject"; message.emailBody = fullErrorMessage; int intSendMailType; intSendMailType = int.Parse(ConfigurationManager.AppSettings["SmtpType"]); switch (intSendMailType) { case 1: message.sendBy = SendMailMessage.SendByEnum.BrinksSmtp; message.smtpServer = ConfigurationManager.AppSettings["SmtpServer"]; break; } SendMail sendMail = new SendMail(); sendMail.MailSend(message); }
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); } } }
private int DownloadEmail(Imap imap, MailProfile profile) { int count = 0; // Build the MailQuery var query = new MailQuery("('Deleted' = 'False')"); var messages = imap.ListMessages(query); foreach (var message in messages) { MailMessage msg = null; msg = imap.FetchMessage(message.UniqueId); var filtertext = Properties.Settings.Default.Filtertext.Split(',').Where(x => x.Trim().Length > 0); if (filtertext.Any(x => msg.Subject.ToLower().Trim().Contains(x.ToLower().Trim()))) { LogProvider.Log(GetType()).Info(string.Format("Bounce Message Found : {0} Subject contains filter text : {1}", msg.Subject, filtertext.Any(x => msg.Subject.ToLower().Contains(x.ToLower())))); //if a message is found check subject has documentid LogProvider.Log(GetType()).Debug("Validating Mail Subject"); if (msg.Subject.ToLower().IndexOf("(resend") > 0) { } else if (msg.Subject.IndexOf('(') > 0) { int DocumentID = Convert.ToInt32(msg.Subject.Substring(msg.Subject.IndexOf('(') + 1, msg.Subject.IndexOf(')') - msg.Subject.IndexOf('(') - 1)); if (Validate(DocumentID, profile.ConnectionString)) { LogProvider.Log(GetType()).Info(string.Format("Message with Document ID {0} found in origin system", DocumentID)); //insert in table for document to be resend using (EmailBounceBackController controller = new EmailBounceBackController()) { ResendEmail dr = new ResendEmail() { MessageID = msg.MessageId, Subject = msg.Subject, DocumentID = DocumentID, OriginalDocumentPath = controller.getDocumentPath(DocumentID, profile.ConnectionString), OriginalEmailSubject = controller.getDocumentSubject(DocumentID, profile.ConnectionString), TimeStamp = DateTime.Now }; controller.InsertDocumentResend(dr); } } // Move the email to the archive (if this fails, but the download is complete this // will just result in a duplicate next time round if the deleted flag is not set) imap.MoveMessage(message.UniqueId, "ResendArchive", true, false); // Increment the download count count++; } else { //The message is not undeliverable or automated response, move to Archive so it will not be picked agian imap.MoveMessage(message.UniqueId, "Archive", true, false); } } else { //The message is not undeliverable or automated response, move to Archive so it will not be picked agian imap.MoveMessage(message.UniqueId, "Archive", true, false); } } return(count); }