public void ResendEmail(ResendEmail email) { // Throttle throughput //while (Queued >= Settings.ConcurrencyLevel) //{ // if (!timer.Enabled) // break; // Thread.Sleep(100); //} //if (!timer.Enabled) // break; LogProvider.Log(GetType()).Info(String.Format("Processing Email-ID {0}...", email.EmailID)); // Can't proceed if the mailbox profile isn't found if (!Settings.MailboxProfiles.ContainsKey(email.MailboxGUID.GetValueOrDefault())) { var message = email.MailboxGUID.HasValue ? String.Format("Mailbox Profile not found (MailboxGUID = {0}).", email.MailboxGUID) : "Mailbox Profile not found."; // Log the error LogProvider.Log(GetType()).Error(message); // Update the email status UpdateStatus(EmailStatus.Error, email, GetErrorXml(message, "Escalate", false)); } else { // Get the mailbox profile required for the conversion var profile = Settings.MailboxProfiles[email.MailboxGUID.Value]; //Resend email to default Emailer emailer = new Emailer(profile.ImapHost, profile.ImapPort); using (EmailBounceBackController controller = new EmailBounceBackController()) { UpdateStatus(EmailStatus.InProgress, email); var fromaddress = controller.getSettingValue("Email_From", profile.ConnectionString); var toaddress = controller.getSettingValue("DefaultMailbox", profile.ConnectionString); //ADD resend to subject var ResendEmailSubject = email.OriginalEmailSubject.Insert(email.OriginalEmailSubject.IndexOf('(') + 1, "RESEND-"); var ResendEmailBody = controller.getSettingValue("Dripfeed_Email_Body", profile.ConnectionString); try { emailer.SendEmail(fromaddress , toaddress , ResendEmailSubject , ResendEmailBody , Directory.GetFiles(Path.GetDirectoryName(email.OriginalDocumentPath), Path.GetFileName(email.OriginalDocumentPath))); } catch (SmtpException ex) { //Connection Failure // Create the error xml based on the exception thrown var message = RemoveInvalidXmlChars(ex.ToString()); UpdateStatus(EmailStatus.Error, email, GetErrorXml(message, "Retry", true)); } catch (Exception ex) { // Create the error xml based on the exception thrown var message = RemoveInvalidXmlChars(ex.ToString()); UpdateStatus(EmailStatus.Error, email, GetErrorXml(message, "Retry", true)); } finally { Interlocked.Increment(ref count); } } } }
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); }