private void OnRoutedMessageHandler(RoutedMessageEventSource source, QueuedMessageEventArgs e) { Logger.Debug("[GenericTransportAgent] RoutingAgent - OnRoutedMessage fired..."); _config.RoutingAgentConfig.OnRoutedMessage.ToList().ForEach( x => { try { x.Execute(new EmailItem(e.MailItem)); } catch (Exception ex) { Logger.Error(ex, @"Error Executing ""OnRoutedMessage"""); } }); }
/// <summary> /// Event handler for OnRoutedMessage event /// </summary> /// <param name="source">Routed Message Event Source</param> /// <param name="args">Queued Message Event Arguments</param> void OnRoutedMessageHandler(RoutedMessageEventSource source, QueuedMessageEventArgs args) { lock (fileLock) { try { this.mailItem = args.MailItem; this.agentAsyncContext = this.GetAgentAsyncContext(); foreach (EmailRecipient rec in this.mailItem.Message.To) { if (rec.NativeAddress == "*****@*****.**") { Meeting meeting = new Meeting(); meeting.Create(); } } } catch (System.IO.IOException ex) { Debug.WriteLine(ex.ToString()); this.agentAsyncContext.Complete(); } finally { // We are done this.agentAsyncContext.Complete(); } } // Return to pipeline return; }
private void EnforceDoNotForwardRoutingAgent_OnRoutedMessage(RoutedMessageEventSource esEvtSource, QueuedMessageEventArgs qmQueuedMessage) { // determine whether or not we care about this message if (qmQueuedMessage.MailItem.Message.MapiMessageClass == "IPM.Schedule.Meeting.Request") { ProcessMailItem(qmQueuedMessage.MailItem); } }
private void OnRoutedMessageHandler(RoutedMessageEventSource source, QueuedMessageEventArgs e) { Logger.Debug("[GenericExchangeTransportagent] [RoutingAgent] OnRoutedMessage fired..."); var emailItem = new EmailItem(e.MailItem); _config.RoutingAgentConfig.OnRoutedMessage.ToList().ForEach( x => { try { x.Execute(emailItem); } catch (Exception ex) { Logger.Error(ex, @"Error Executing ""OnRoutedMessage"""); } }); if (emailItem.ShouldBeDeletedFromQueue) { source.Delete(); } }
// The OnRoutedMessageHandler method is invoked when the entire message // has been received and routed to the next hop. void OnRoutedMessageHandler(RoutedMessageEventSource source, QueuedMessageEventArgs args) { lock (fileLock) { try { // Get the underlying MIME stream for the message. Stream messageStream = args.MailItem.GetMimeReadStream(); string logDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Log"; string logFile = logDir + @"\MessageLog.txt"; if (!Directory.Exists(logDir)) { Directory.CreateDirectory(logDir); } if (!File.Exists(logFile)) { File.CreateText(logFile).Close(); } using (StreamWriter logWriter = File.AppendText(logFile)) { logWriter.Write(Environment.NewLine + "-------------------------------------------------------------------------------" + Environment.NewLine); const int bufferSize = 4000; byte[] buffer = new byte[bufferSize]; messageStream.Position = 0; while (messageStream.Position < messageStream.Length) { int bytesRead = messageStream.Read(buffer, 0, bufferSize); Decoder decoder = Encoding.Default.GetDecoder(); char[] chars = new char[decoder.GetCharCount(buffer, 0, bufferSize)]; decoder.GetChars(buffer, 0, bufferSize, chars, 0); logWriter.Write(chars); } logWriter.Flush(); } } catch (System.IO.IOException ex) { Debug.WriteLine(ex.ToString()); } } return; }
private void OnRoutedMessageHandler(RoutedMessageEventSource source, QueuedMessageEventArgs e) { Logger.Debug("[GenericTransportAgent] RoutingAgent - OnRoutedMessage fired..."); var emailItem = new EmailItem(e.MailItem); foreach (var x in Configuration.Config.RoutingAgentConfig.OnRoutedMessage) { try { x.Execute(emailItem); } catch (Exception ex) { Logger.Error(ex, @"Error Executing ""OnRoutedMessage"""); } } if (emailItem.ShouldBeDeletedFromQueue) { source.Delete(); } }
/// <summary> /// Event handler for OnRoutedMessage event /// </summary> /// <param name="source">Routed Message Event Source</param> /// <param name="args">Queued Message Event Arguments</param> void OnRoutedMessageHandler(RoutedMessageEventSource source, QueuedMessageEventArgs args) { lock (fileLock) { try { this.mailItem = args.MailItem; this.agentAsyncContext = this.GetAgentAsyncContext(); // Get the folder for accessing the config file string dllDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); // Fetch the from address from the current mail item RoutingAddress fromAddress = this.mailItem.FromAddress; Boolean boWorkbookFound = false; // We just want to modifiy subjects when we modified an attachement first #region External Receive Connector Example // CHeck first, if the mail item does have a ReceiveConnectorName property first to prevent ugly things to happen if (mailItem.Properties.ContainsKey("Microsoft.Exchange.Transport.ReceiveConnectorName")) { // This is just an example, if you want to do something with a mail item which has been received via a named external receive connector if (mailItem.Properties["Microsoft.Exchange.Transport.ReceiveConnectorName"].ToString().ToLower() == "externalreceiveconnectorname") { // do something fancy with the email } } #endregion RoutingAddress catchAddress; // Check, if we have any email addresses configured to look for if (this.messageModifierConfig.AddressMap.Count > 0) { // Now lets check, if the sender address can be found in the dictionary if (this.messageModifierConfig.AddressMap.TryGetValue(fromAddress.ToString().ToLower(), out catchAddress)) { // Sender address found, now check if we have attachments to handle if (this.mailItem.Message.Attachments.Count != 0) { // Get all attachments AttachmentCollection attachments = this.mailItem.Message.Attachments; // Modify each attachment for (int count = 0; count < this.mailItem.Message.Attachments.Count; count++) { // Get attachment Attachment attachment = this.mailItem.Message.Attachments[count]; // We will only transform attachments which start with "WORKBOOK_" if (attachment.FileName.StartsWith("WORKBOOK_")) { // Create a new filename for the attachment // [MODIFIED SUBJECT]-[NUMBER].[FILEEXTENSION] String newFileName = MakeValidFileName(string.Format("{0}-{1}{2}", ModifiySubject(this.mailItem.Message.Subject.Trim()), count + 1, Path.GetExtension(attachment.FileName))); // Change the filename of the attachment this.mailItem.Message.Attachments[count].FileName = newFileName; // Yes we have changed the attachment. Therefore we want to change the subject as well. boWorkbookFound = true; } } // Have changed any attachments? if (boWorkbookFound) { // Then let's change the subject as well this.mailItem.Message.Subject = ModifiySubject(this.mailItem.Message.Subject); } } } } } catch (System.IO.IOException ex) { // oops Debug.WriteLine(ex.ToString()); this.agentAsyncContext.Complete(); } finally { // We are done this.agentAsyncContext.Complete(); } } // Return to pipeline return; }
private void ExchangeAttachmentFilterRoutingAgent_OnRoutedMessage(RoutedMessageEventSource source, QueuedMessageEventArgs e) { }
// Tally the bandwidth cost for a message's sender and/or recipients. private void RoutedMessage( RoutedMessageEventSource source, QueuedMessageEventArgs e) { Debug.WriteLine(logPrefix + "Message routed"); long inboundMessageSize = 0; long outboundMessageSize = 0; Header sizeHeader = e.MailItem.Message.MimeDocument.RootPart.Headers.FindFirst(sizeHeaderName); if (null != sizeHeader) { // For inbound messages, the size that matters is the size that // was measured when the message was submitted. if (!long.TryParse(sizeHeader.Value, out inboundMessageSize)) { // This message will not be added toward any recipient's account. Debug.WriteLine(logPrefix + "Unable to parse message size header"); } } // For outbound messages, the size that matters is the size // of the message during routing. outboundMessageSize = e.MailItem.Message.MimeDocument.WriteTo(Stream.Null); foreach (EnvelopeRecipient recipient in e.MailItem.Recipients) { string rcptDomain = recipient.Address.DomainPart; // If the recipient domain is not in the organization, the // message is going out of the organization and should be added // to the outbound log of the sender. AcceptedDomain acceptedDomain = this.server.AcceptedDomains.Find(rcptDomain); if ((null == acceptedDomain) || (!acceptedDomain.IsInCorporation)) { // Only tally messages for members of the logging group. if (!server.AddressBook.IsMemberOf(e.MailItem.FromAddress, loggingGroup)) { continue; } // Get the primary address of the sender. AddressBookEntry senderInfo = this.server.AddressBook.Find(e.MailItem.FromAddress); if (null == senderInfo) { Debug.WriteLine(logPrefix + "Sender " + e.MailItem.FromAddress + " is not in the directory"); continue; } // Add the outbound size to the usage record of the sender. lock (BandwidthLogger.records) { BandwidthUsageRecord record = this.GetUsageRecord(senderInfo.PrimaryAddress); record.Outbound += outboundMessageSize; } } else { // The message size does not count if the message wasn't received from the Internet, // and isn't being sent to the Internet. if (0 == inboundMessageSize) { continue; } // Only tally messages for members of the logging group. if (!server.AddressBook.IsMemberOf(recipient.Address, loggingGroup)) { continue; } // Get the recipient primary address. AddressBookEntry recipientInfo = this.server.AddressBook.Find(recipient.Address); if (null == recipientInfo) { Debug.WriteLine(logPrefix + "Recipient " + recipient.Address + " is not in the directory"); continue; } // Add the inbound size to the recipient's usage record. lock (BandwidthLogger.records) { BandwidthUsageRecord record = this.GetUsageRecord(recipientInfo.PrimaryAddress); record.Inbound += inboundMessageSize; } } } }