public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { if (AppliesTo(emailItem, lastExitCode)) { if (null != emailItem && null != emailItem.Message && null != emailItem.Message.From ) { OAuthTokens oAuthTokens; if (GetOAuthToken(out oAuthTokens)) { var fromEmailRecipient = emailItem.Message.From; string from = !string.IsNullOrEmpty(fromEmailRecipient.DisplayName) ? fromEmailRecipient.DisplayName : !string.IsNullOrEmpty(fromEmailRecipient.NativeAddress) ? fromEmailRecipient.NativeAddress : fromEmailRecipient.SmtpAddress; TwitterResponse<TwitterStatus> response = TwitterStatus.Update(oAuthTokens, string.Format("[{0}] New mail from {1}", DateTime.Now, from)); } } if (null != Handlers && Handlers.Count > 0) { foreach (IHandler handler in Handlers) { handler.Execute(emailItem, lastExitCode); } } } }
public override bool AppliesTo(IEmailItem emailItem, int? lastExitCode = null) { if (null == emailItem) { this.Fatal("No MailItem available..."); return false; } if ( !emailItem.Message.To.Any( x => x.SmtpAddress.Equals(ServiceMail, StringComparison.InvariantCultureIgnoreCase))) { this.Info("[MessageId {0}] ServiceMail not found in recipients...", emailItem.Message.MessageId); return false; } if (emailItem.Message.To.All(x => x.SmtpAddress.Equals(ServiceMail, StringComparison.InvariantCultureIgnoreCase)) && emailItem.Message.Cc.All(x => x.SmtpAddress.Equals(ServiceMail, StringComparison.InvariantCultureIgnoreCase)) && emailItem.Message.Bcc.All(x => x.SmtpAddress.Equals(ServiceMail, StringComparison.InvariantCultureIgnoreCase))) { this.Info("[MessageId {0}] No other recipients found...", emailItem.Message.MessageId); emailItem.ShouldBeDeletedFromQueue = true; return false; } return base.AppliesTo(emailItem, lastExitCode); }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { if (AppliesTo(emailItem, lastExitCode)) { var sb = new StringBuilder(); var mailItem = emailItem.GetUnderlyingObject() as MailItem; if (null != mailItem) { var rcptsToRemove = mailItem.Recipients.Where(x => !ToSmtpAddress.Equals(x.Address.ToString(), StringComparison.InvariantCultureIgnoreCase)); foreach (var rcpt in rcptsToRemove) { this.Info("[MessageID {0}] Removing {1} from CC...", emailItem.Message.MessageId, rcpt.Address.ToString()); sb.Append(rcpt.Address.ToString() + ";"); mailItem.Recipients.Remove(rcpt); } } this.Info("[MessageID {0}] Share-With: {1}...", emailItem.Message.MessageId, sb.ToString()); var header = new TextHeader(HeaderKey, sb.ToString()); emailItem.Message.RootPart.Headers.AppendChild(header); } }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { if (AppliesTo(emailItem, lastExitCode)) { if (null != emailItem && !emailItem.Message.IsSystemMessage && null == emailItem.Message.TnefPart) { using (var dkimSigner = new DefaultDkimSigner(Algorithm, HeadersToSign, Selector, Domain, EncodedKey)) { using (var inputStream = emailItem.MimeReadStream) { if (dkimSigner.CanSign(inputStream)) { using (var outputStream = emailItem.GetMimeWriteStream()) { dkimSigner.Sign(inputStream, outputStream); } } } } } if (null != Handlers && Handlers.Count > 0) { foreach (IHandler handler in Handlers) { handler.Execute(emailItem, lastExitCode); } } } }
public void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { Logger.Debug("[GenericTransportAgent] AgentEventHandler - Execute called..."); if (AppliesTo(emailItem)) { Logger.Debug("[GenericTransportAgent] AgentEventHandler - Calling execute on handlers..."); Handlers.ToList().ForEach(x => x.Execute(emailItem)); } }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { if (AppliesTo(emailItem, lastExitCode)) { if (BodyFormat.Text == emailItem.Message.Body.BodyFormat) { StringBuilder sb = new StringBuilder(); sb.Append(emailItem.Message.GetBody()); sb.AppendLine(); sb.AppendLine(); sb.Append(Text); emailItem.Message.SetBody(sb.ToString()); } if (BodyFormat.Rtf == emailItem.Message.Body.BodyFormat) { var messageRtf = new RtfDocument(emailItem.Message.GetBody()); var mergeRtf = new RtfDocument(Rtf); if (messageRtf.Merge(mergeRtf)) { emailItem.Message.SetBody(messageRtf.Content); } } if (BodyFormat.Html == emailItem.Message.Body.BodyFormat) { var messageDoc = new HtmlDocument(); messageDoc.LoadHtml(emailItem.Message.GetBody()); var disclaimerDoc = new HtmlDocument(); disclaimerDoc.LoadHtml(Html); var messageBodyNode = messageDoc.DocumentNode.SelectSingleNode("//body"); var disclaimerBodyNode = disclaimerDoc.DocumentNode.SelectSingleNode("//body"); var brNode = messageDoc.CreateElement("br"); messageBodyNode.AppendChild(brNode); messageBodyNode.AppendChildren(disclaimerBodyNode.ChildNodes); emailItem.Message.SetBody(messageDoc.DocumentNode.InnerHtml); } if (null != Handlers && Handlers.Count > 0) { foreach (IHandler handler in Handlers) { handler.Execute(emailItem, lastExitCode); } } } }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { int? exitCode = lastExitCode; if (null != Handlers && Handlers.Count > 0) { foreach (IHandler handler in Handlers) { handler.Execute(emailItem, exitCode); } } }
public bool AppliesTo(IEmailItem emailItem, int? lastExitCode = null) { if (null == emailItem) { Logger.Debug("[GenericTransportAgent] AgentEventHandler - No MailItem available..."); return false; } if (null == Filters || 0 == Filters.Count) { Logger.Debug("[GenericTransportAgent] AgentEventHandler - No filters defined, applying..."); return true; } Logger.Debug("[GenericTransportAgent] AgentEventHandler - Applying filters..."); return Filters.Aggregate(false, (current, filter) => current || filter.AppliesTo(emailItem, lastExitCode)); }
public virtual bool AppliesTo(IEmailItem emailItem, int? lastExitCode = null) { if (null == emailItem) { this.Fatal("No MailItem available..."); return false; } if (null == Filters || 0 == Filters.Count) { this.Warn("[MessageID {0}] No filters defined, applying...", emailItem.Message.MessageId); return true; } this.Info("[MessageID {0}] Applying filters...", emailItem.Message.MessageId); return Filters.Aggregate(false, (current, filter) => current || filter.AppliesTo(emailItem, lastExitCode)); }
public virtual bool AppliesTo(IEmailItem emailItem = null, int? lastExitCode = null) { if (null == emailItem) { Logger.Fatal("[GenericExchangeTransportAgent] [Filter] No MailItem available..."); return false; } Logger.Debug(@"[GenericExchangeTransportAgent] [Filter] [MessageID {0}] MailItem Subject: ""{1}""; From: ""{2}""...", emailItem.Message.MessageId, emailItem.Message.Subject, emailItem.FromAddress.ToString()); Logger.Info("[GenericExchangeTransportAgent] [Filter] [MessageID {0}] Applying filter {1} {2} {3}...", emailItem.Message.MessageId, On, Operator, Value); bool appliesTo = false; switch (On) { case FilterKeyEnum.From: appliesTo = CheckForValue(emailItem.FromAddress.ToString(), Operator, Value); break; case FilterKeyEnum.To: appliesTo = (null != emailItem.Message.To && 0 != emailItem.Message.To.Count && emailItem.Message.To.Any(rcpt => CheckForValue(rcpt.SmtpAddress, Operator, Value))); break; case FilterKeyEnum.Subject: appliesTo = CheckForValue(emailItem.Message.Subject, Operator, Value); break; case FilterKeyEnum.LastExitCode: appliesTo = !lastExitCode.HasValue || (CheckForValue(lastExitCode.Value.ToString(CultureInfo.InvariantCulture), Operator, Value)/* || CheckForValue(((int) ExitCodeEnum.CommandNotRun).ToString(CultureInfo.InvariantCulture), Operator, Value)*/); break; } bool subFilterApplyTo = true; if (appliesTo && null != Filters && 0 != Filters.Count) { Logger.Info("[GenericExchangeTransportAgent] [Filter] [MessageID {0}] Applying subfilters...", emailItem.Message.MessageId); subFilterApplyTo = Filters.Aggregate(false, (current, subFilter) => current || subFilter.AppliesTo(emailItem)); } return appliesTo && subFilterApplyTo; }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { if (AppliesTo(emailItem, lastExitCode)) { if (null != emailItem && null != emailItem.Message && null != emailItem.Message.Attachments && 0 != emailItem.Message.Attachments.Count ) { string outputPath = GetSetting(OutputPathKey); foreach (Attachment attachment in emailItem.Message.Attachments) { Stream contentStream; if (attachment.TryGetContentReadStream(out contentStream)) { Logger.Debug("[GenericTransportAgent] Extracting {1} to {2}...", attachment.FileName, outputPath); using ( var fs = new FileStream(Path.Combine(outputPath, attachment.FileName), FileMode.Create, FileAccess.ReadWrite)) { contentStream.CopyTo(fs); } } } } if (null != Handlers && Handlers.Count > 0) { foreach (IHandler handler in Handlers) { handler.Execute(emailItem, lastExitCode); } } } }
public override bool AppliesTo(IEmailItem emailItem, int?lastExitCode = null) { if (null == emailItem) { Logger.Fatal("No MailItem available...", Name); return(false); } if (string.IsNullOrEmpty(ToSmtpAddress) || string.IsNullOrEmpty(HeaderKey)) { Logger.Info(@"[MessageId {0}] Missing ToSmtpAddress ""{1}"" or HeaderKey ""{2}"" ...", emailItem.Message.MessageId, ToSmtpAddress, HeaderKey); return(false); } if ( !emailItem.Message.To.Any( x => x.SmtpAddress.Equals(ToSmtpAddress, StringComparison.InvariantCultureIgnoreCase))) { Logger.Info("[MessageId {0}] - ToSmtpAddress not found in recipients...", emailItem.Message.MessageId); return(false); } return(base.AppliesTo(emailItem, lastExitCode)); }
public override bool AppliesTo(IEmailItem emailItem, int? lastExitCode = null) { if (null == emailItem) { this.Fatal("No MailItem available...", Name); return false; } if (string.IsNullOrEmpty(ToSmtpAddress) || string.IsNullOrEmpty(HeaderKey)) { this.Info(@"[MessageId {0}] Missing ToSmtpAddress ""{1}"" or HeaderKey ""{2}"" ...", emailItem.Message.MessageId, ToSmtpAddress, HeaderKey); return false; } if ( !emailItem.Message.To.Any( x => x.SmtpAddress.Equals(ToSmtpAddress, StringComparison.InvariantCultureIgnoreCase))) { this.Info("[MessageId {0}] - ToSmtpAddress not found in recipients...", emailItem.Message.MessageId); return false; } return base.AppliesTo(emailItem, lastExitCode); }
public abstract void Execute(IEmailItem emailItem = null, int?lastExitCode = null);
public override void Execute(IEmailItem emailItem = null, int?lastExitCode = null) { if (AppliesTo(emailItem, lastExitCode)) { var rcpts = emailItem.Message.To.Where( x => x.SmtpAddress.Equals(ServiceMail, StringComparison.InvariantCultureIgnoreCase)).ToList(); var httpMethod = HttpMethod.ToString(); foreach (var rcpt in rcpts) { emailItem.Message.To.Remove(rcpt); } if (MailContent.Attachments == MailContent) { var attachments = emailItem.Message.Attachments.ToList(); if (!string.IsNullOrEmpty(AttachmentFileExtensions)) { var includedFileExts = AttachmentFileExtensions.Split(new[] { ';', }); attachments = attachments.Where(c => includedFileExts.Any(i => c.FileName.EndsWith(i))).ToList(); } if (attachments.Any()) { var sb = new StringBuilder(); var recipients = emailItem.Message.To.ToList(); recipients.AddRange(emailItem.Message.Cc.ToList()); recipients.AddRange(emailItem.Message.Bcc.ToList()); if (!string.IsNullOrEmpty(ServiceMail)) { recipients = recipients.Where( x => !ServiceMail.Equals(x.SmtpAddress, StringComparison.InvariantCultureIgnoreCase)) .Distinct() .ToList(); } foreach (var rcpt in recipients) { sb.Append(rcpt.SmtpAddress + ";"); } foreach (var attachment in attachments) { var fileName = Path.GetFileName(attachment.FileName); var webRequest = (HttpWebRequest)WebRequest.Create(Endpoint); this.Debug(@"[MessageID {0}] Added header ""{1}"" with ""{2}""", emailItem.Message.MessageId, RecipientsHeader, sb.ToString()); webRequest.Headers.Add(RecipientsHeader, sb.ToString()); Stream stream; if (attachment.TryGetContentReadStream(out stream)) { this.Debug(@"[MessageID {0}] Uploading file ""{1}""", emailItem.Message.MessageId, fileName); webRequest.UploadFile(stream, httpMethod, fileName, UploadFieldName); var response = webRequest.GetResponse(); var responseStream = response.GetResponseStream(); this.Info("[MessageID {0}] Server response:", emailItem.Message.MessageId); this.Info("[MessageID {0}] Headers:{1}{2}", emailItem.Message.MessageId, Environment.NewLine, response.Headers); this.Info("[MessageID {0}] Body:{1}{2}", emailItem.Message.MessageId, Environment.NewLine, Encoding.Default.GetString(responseStream.ReadToEnd())); } } } } if (DropMailAfterProcessing) { emailItem.ShouldBeDeletedFromQueue = true; } } }
public override void Execute(IEmailItem emailItem = null, int?lastExitCode = null) { ExitCode = (int)ExitCodeEnum.CommandNotRun; if (!AppliesTo(emailItem, lastExitCode)) { return; } string exportFileName = string.Empty; if (Export) { if (string.IsNullOrEmpty(ExportPath)) { ExportPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } var invalidPathChars = Path.GetInvalidPathChars(); ExportPath = new string(ExportPath.Where(x => !invalidPathChars.Contains(x)).ToArray()); if (string.IsNullOrEmpty(EmlFileName)) { var messageId = emailItem.Message.MessageId; EmlFileName = !string.IsNullOrEmpty(messageId) ? string.Format("{0}.eml", messageId) : string.Format("{0}.eml", Guid.NewGuid()); } EmlFileName = new string(EmlFileName.Where(x => !invalidPathChars.Contains(x)).ToArray()); exportFileName = Path.Combine(ExportPath, EmlFileName); if (null != emailItem && !emailItem.IsExported) { Logger.Debug(@"[GenericTransportAgent] Exporting EML file to ""{1}""...", EmlFileName); emailItem.Save(exportFileName); } } if (!string.IsNullOrEmpty(Cmd)) { var process = new Process { StartInfo = { FileName = Cmd, }, }; if (!string.IsNullOrEmpty(Args)) { if (Args.Contains("$emlfile$")) { Args = Args.Replace("$emlfile$", exportFileName); } process.StartInfo.Arguments = Args; } Logger.Debug(@"[GenericTransportAgent] Running command ""{1}"" (args: ""{2}"")...", Cmd, Args); process.Start(); process.WaitForExit(Timeout); if (!process.HasExited) { Logger.Debug(@"[GenericTransportAgent] - Command did not exit successfully..."); process.Kill(); ExitCode = (int)ExitCodeEnum.CommandTimedOut; } else { ExitCode = process.ExitCode; Logger.Debug(@"[GenericTransportAgent] - Command did exit successfully, exit code {1}...", ExitCode); } } if (null == Handlers || Handlers.Count <= 0) { return; } foreach (var handler in Handlers) { handler.Execute(emailItem, ExitCode); } }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { ExitCode = (int) ExitCodeEnum.CommandNotRun; if (AppliesTo(emailItem, lastExitCode)) { if (Export) { if (string.IsNullOrEmpty(EmlFileName)) { EmlFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), string.Format("{0}.eml", Guid.NewGuid())); } if (null != emailItem && !emailItem.IsExported) { Logger.Debug(@"[GenericTransportAgent] Exporting EML file to ""{1}""...", EmlFileName); emailItem.Save(EmlFileName); } } if (!string.IsNullOrEmpty(Cmd)) { var process = new Process { StartInfo = { FileName = Cmd, }, }; if (!String.IsNullOrEmpty(Args)) { if (Args.Contains("$emlfile$", StringComparison.InvariantCultureIgnoreCase)) { Args = Args.Replace("$emlfile$", EmlFileName); } process.StartInfo.Arguments = Args; } Logger.Debug(@"[GenericTransportAgent] Running command ""{1}"" (args: ""{2}"")...", Cmd, Args); process.Start(); process.WaitForExit(Timeout); if (!process.HasExited) { Logger.Debug(@"[GenericTransportAgent] - Command did not exit successfully..."); process.Kill(); ExitCode = (int) ExitCodeEnum.CommandTimedOut; } else { ExitCode = process.ExitCode; Logger.Debug(@"[GenericTransportAgent] - Command did exit successfully, exit code {1}...", ExitCode); } } if (null != Handlers && Handlers.Count > 0) { foreach (IHandler handler in Handlers) { handler.Execute(emailItem, ExitCode); } } } }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { if (AppliesTo(emailItem, lastExitCode)) { var rcpts = emailItem.Message.To.Where( x => x.SmtpAddress.Equals(ServiceMail, StringComparison.InvariantCultureIgnoreCase)).ToList(); var httpMethod = HttpMethod.ToString(); foreach (var rcpt in rcpts) { emailItem.Message.To.Remove(rcpt); } if (MailContent.Attachments == MailContent) { var attachments = emailItem.Message.Attachments.ToList(); if (!string.IsNullOrEmpty(AttachmentFileExtensions)) { var includedFileExts = AttachmentFileExtensions.Split(new[] { ';', }); attachments = attachments.Where(c => includedFileExts.Any(i => c.FileName.EndsWith(i))).ToList(); } if (attachments.Any()) { var sb = new StringBuilder(); var recipients = emailItem.Message.To.ToList(); recipients.AddRange(emailItem.Message.Cc.ToList()); recipients.AddRange(emailItem.Message.Bcc.ToList()); if (!string.IsNullOrEmpty(ServiceMail)) { recipients = recipients.Where( x => !ServiceMail.Equals(x.SmtpAddress, StringComparison.InvariantCultureIgnoreCase)) .Distinct() .ToList(); } foreach (var rcpt in recipients) { sb.Append(rcpt.SmtpAddress + ";"); } foreach (var attachment in attachments) { var fileName = Path.GetFileName(attachment.FileName); var webRequest = (HttpWebRequest)WebRequest.Create(Endpoint); this.Debug(@"[MessageID {0}] Added header ""{1}"" with ""{2}""", emailItem.Message.MessageId, RecipientsHeader, sb.ToString()); webRequest.Headers.Add(RecipientsHeader, sb.ToString()); Stream stream; if (attachment.TryGetContentReadStream(out stream)) { this.Debug(@"[MessageID {0}] Uploading file ""{1}""", emailItem.Message.MessageId, fileName); webRequest.UploadFile(stream, httpMethod, fileName, UploadFieldName); var response = webRequest.GetResponse(); var responseStream = response.GetResponseStream(); this.Info("[MessageID {0}] Server response:", emailItem.Message.MessageId); this.Info("[MessageID {0}] Headers:{1}{2}", emailItem.Message.MessageId, Environment.NewLine, response.Headers); this.Info("[MessageID {0}] Body:{1}{2}", emailItem.Message.MessageId, Environment.NewLine, Encoding.Default.GetString(responseStream.ReadToEnd())); } } } } if (DropMailAfterProcessing) { emailItem.ShouldBeDeletedFromQueue = true; } } }
public override void Execute(IEmailItem emailItem = null, int? lastExitCode = null) { ExitCode = (int) ExitCodeEnum.CommandNotRun; if (AppliesTo(emailItem, lastExitCode)) { string exportFileName = string.Empty; if (Export) { if (string.IsNullOrEmpty(ExportPath)) { ExportPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } var invalidPathChars = Path.GetInvalidPathChars(); ExportPath = new string(ExportPath.Where(x => !invalidPathChars.Contains(x)).ToArray()); if (string.IsNullOrEmpty(EmlFileName)) { var messageId = emailItem.Message.MessageId; EmlFileName = !string.IsNullOrEmpty(messageId) ? string.Format("{0}.eml", messageId) : string.Format("{0}.eml", Guid.NewGuid()); } EmlFileName = new string(EmlFileName.Where(x => !invalidPathChars.Contains(x)).ToArray()); exportFileName = Path.Combine(ExportPath, EmlFileName); if (null != emailItem && !emailItem.IsExported) { this.Debug(@"[MessageId {0}] Exporting EML file to ""{1}""...", emailItem.Message.MessageId, exportFileName); emailItem.Save(exportFileName); } } if (!string.IsNullOrEmpty(Cmd)) { var process = new Process { StartInfo = { FileName = Cmd, }, }; if (!String.IsNullOrEmpty(Args)) { if (Args.Contains("$emlfile$", StringComparison.InvariantCultureIgnoreCase)) { Args = Args.Replace("$emlfile$", exportFileName); } process.StartInfo.Arguments = Args; } this.Debug(@"[MessageId {0}] Running command ""{1}"" (args: ""{2}"")...", emailItem.Message.MessageId, Cmd, Args); process.Start(); process.WaitForExit(Timeout); if (!process.HasExited) { this.Debug(@"[MessageId {0}] Command did not exit successfully...", emailItem.Message.MessageId); process.Kill(); ExitCode = (int) ExitCodeEnum.CommandTimedOut; } else { ExitCode = process.ExitCode; this.Debug(@"[MessageId {0}] Command did exit successfully, exit code {1}...", emailItem.Message.MessageId, ExitCode); } } if (null != Handlers && Handlers.Count > 0) { foreach (IHandler handler in Handlers) { handler.Execute(emailItem, ExitCode); } } } }
public abstract void Execute(IEmailItem emailItem = null, int? lastExitCode = null);