public void RemoveRecipients() { MessageEnvelope envelope = new MessageEnvelope(TestMessage); DirectAddressCollection toRemove = new DirectAddressCollection(); toRemove.Add(new DirectAddress("<*****@*****.**>")); toRemove.Add(new DirectAddress("<*****@*****.**>")); toRemove.Add(new DirectAddress("<*****@*****.**>")); toRemove.Add(new DirectAddress("<*****@*****.**>")); Assert.DoesNotThrow(() => envelope.RemoveFromRoutingHeaders(toRemove)); string outputText = null; Assert.DoesNotThrow(() => outputText = envelope.SerializeMessage()); Message outputMessage = Message.Load(outputText); Assert.True(outputMessage.Bcc == null); this.CheckHeaderRaw(outputMessage.Cc, 2); this.CheckCollection(outputMessage.Cc, new string[] {"*****@*****.**", "*****@*****.**"}, new string[] {"*****@*****.**>", "*****@*****.**"}); this.CheckHeaderRaw(outputMessage.To, 1); this.CheckCollection(outputMessage.To, new string[] { "*****@*****.**"}, new string[] { "*****@*****.**"}); }
protected virtual void RejectMessage(ISmtpMessage message, MessageEnvelope envelope, bool? incoming) { try { message.Reject(); Auditor.ForEach(a => a.Log(AuditNames.Message.GetRejectedMessage(incoming), a.BuildAuditLogMessage.Build(message))); Logger.Debug("Rejected Message"); if (!incoming.GetValueOrDefault(false) && envelope.ShouldDeliverFailedStatus(m_settings.Notifications)) { var outgoingMessage = BuildFailedOutgoingMessage(envelope); SendDeliveryStatus(outgoingMessage); } else { this.CopyMessage(message, m_settings.BadMessage); } } catch { } }
protected virtual void UpdateMessageText(ISmtpMessage message, MessageEnvelope envelope) { string messageText = envelope.SerializeMessage(); if (string.IsNullOrEmpty(messageText)) { throw new SmtpAgentException(SmtpAgentError.EmptyResultFromAgent); } message.Update(messageText); }
//--------------------------------------------------- // // Message Manipulation // //--------------------------------------------------- protected virtual void UpdateXHeaders(MessageEnvelope envelope) { if (envelope is IncomingMessage && envelope.HasDomainRecipients) { Message message = envelope.Message; // // Inject the domain recipients & verified sender from the envelope into the message using an x-receiver + x-sender headers // These will be useful after the message is serialized and then deserialized for further processing // message.Headers.SetValue(XHeaders.Receivers, envelope.DomainRecipients.ToString()); message.Headers.SetValue(XHeaders.Sender, envelope.Sender.ToString()); } }
protected virtual MessageEnvelope ProcessIncoming(ISmtpMessage message, MessageEnvelope envelope) { envelope = this.SecurityAgent.ProcessIncoming(envelope); Logger.Debug("ProcessedIncoming"); return envelope; }
internal IncomingMessage(MessageEnvelope envelope) : base(envelope) { }
public void TestUntrusted() { // // This should be accepted because the envelope is what we look at // MessageEnvelope envelope = new MessageEnvelope(BadMessage, DirectAddressCollection.ParseSmtpServerEnvelope("*****@*****.**"), new DirectAddress("*****@*****.**") ); Assert.DoesNotThrow(() => m_agent.SecurityAgent.ProcessOutgoing(envelope)); envelope = new MessageEnvelope(string.Format(TestMessage, Guid.NewGuid()), DirectAddressCollection.ParseSmtpServerEnvelope("*****@*****.**"), new DirectAddress("*****@*****.**")); // // This SHOULD throw an exception // Assert.Throws<OutgoingAgentException>(() => m_agent.SecurityAgent.ProcessOutgoing(envelope)); }
/// <summary> /// Decrypts and verifies trust in a MessageEnvelope instance with signed and encrypted message content. /// </summary> /// <param name="envelope"> /// A <see cref="MessageEnvelope"/> instance with signed and encrypted content for decryption and trust verification. /// </param> /// <returns> /// An <see cref="IncomingMessage"/> instance with the trust verified decrypted and verified message. /// </returns> public IncomingMessage ProcessIncoming(MessageEnvelope envelope) { if (envelope == null) { throw new ArgumentNullException("envelope"); } return this.ProcessIncoming(new IncomingMessage(envelope)); }
protected virtual MessageEnvelope ProcessEnvelope(ISmtpMessage message, MessageEnvelope envelope) { // // OUTGOING: // Non-Encrypted messages from within the domain are treated as OUTGOING. // Encrypted messages from within the domain are OPTIONALLY treated as Incoming // - Only if InternalRelay is enabled // INCOMING: // All messages sent by sources OUTSIDE the domain are ALWAYS treated as INCOMING // // The following boolean logic is the way it is to make it *easy to read* // bool isSenderInDomain = this.SecurityAgent.Domains.IsManaged(envelope.Sender); bool isOutgoing; if (isSenderInDomain) { isOutgoing = true; if (SMIMEStandard.IsEncrypted(envelope.Message)) { if (!m_settings.AllowInternalRelay) { throw new SmtpAgentException(SmtpAgentError.InternalRelayDisabled); } isOutgoing = false; } } else { isOutgoing = false; } if (isOutgoing) { envelope = this.ProcessOutgoing(message, envelope); } else { envelope = this.ProcessIncoming(message, envelope); } if (envelope == null) { throw new SmtpAgentException(SmtpAgentError.InvalidEnvelopeFromAgent); } return envelope; }
private void HandleMessageRejection(ISmtpMessage message, MessageEnvelope envelope, bool? isIncoming, Exception ex) { if (envelope != null && ex is OutgoingAgentException && ((OutgoingAgentException)ex).Error == AgentError.NoTrustedRecipients) { this.RejectMessage(message, envelope, isIncoming); } else { this.RejectMessage(message, isIncoming); Logger.Error("While processing message {0}", ex.ToString()); } }
MessageEnvelope CreateEnvelope(CDO.Message message) { DirectAddressCollection recipientAddresses = null; DirectAddress senderAddress = null; MessageEnvelope envelope; string messageText = message.GetMessageText(); if (this.ExtractEnvelopeFields(message, ref recipientAddresses, ref senderAddress)) { envelope = new MessageEnvelope(messageText, recipientAddresses, senderAddress); } else { envelope = new MessageEnvelope(messageText); } return envelope; }
internal void SummarizeHeaders(StringBuilder builder, MessageEnvelope envelope) { if (envelope.HasRecipients) { builder.Append("RECIPIENTS=").AppendLine(envelope.Recipients); } if (envelope.HasDomainRecipients) { builder.Append("DOMAIN RECIPIENTS=").AppendLine(envelope.DomainRecipients); } if (envelope.HasRejectedRecipients) { builder.Append("REJECTED RECIPIENTS=").AppendLine(envelope.RejectedRecipients); builder.Append("NO CERTS=").AppendLine(this.CollectNoCertInformation(envelope.RejectedRecipients)); } if (envelope.HasOtherRecipients) { builder.Append("OTHER RECIPIENTS=").AppendLine(envelope.OtherRecipients); } IncomingMessage incoming = envelope as IncomingMessage; if (incoming != null) { CollectSignatures(builder, incoming); } }
internal string BuildVerboseErrorMessage(string message, MessageEnvelope envelope, Exception ex) { StringBuilder builder = new StringBuilder(); builder.AppendLine(message); this.SummarizeHeaders(builder, envelope); builder.AppendLine(ex.ToString()); return builder.ToString(); }
/// <summary> /// Create a new OutgoingMessage envelope from the given message envelope /// </summary> /// <param name="envelope">source envelope</param> public OutgoingMessage(MessageEnvelope envelope) : base(envelope) { }
private OutgoingMessage BuildFailedOutgoingMessage(MessageEnvelope envelope) { return new OutgoingMessage( envelope.Message, envelope.Recipients, //not used but required for validation envelope.Recipients, //All are rejected. envelope.Sender, envelope.ShouldDeliverFailedStatus(m_settings.Notifications)); }
internal MessageEnvelope(MessageEnvelope envelope) { m_agent = envelope.m_agent; this.RawMessage = envelope.RawMessage; m_message = envelope.m_message; if (envelope.m_recipients != null) { m_recipients = new DirectAddressCollection {envelope.m_recipients}; } m_sender = envelope.m_sender; m_notifyTo = envelope.m_notifyTo; }
protected virtual void PostProcessMessage(ISmtpMessage message, MessageEnvelope envelope) { OutgoingMessage outgoing = envelope as OutgoingMessage; if (outgoing != null) { this.PostProcessOutgoing(message, outgoing); } else { this.PostProcessIncoming(message, (IncomingMessage) envelope); } }
/// <summary> /// Encrypts, verifies recipient trust, and signs a MessageEnvelope containing a message to prepare for send. /// </summary> /// <param name="envelope"> /// A <see cref="MessageEnvelope"/> instance containing the message to prepare for send. /// </param> /// <returns> /// An <see cref="OutgoingMessage"/> instance containing the encrypted and trust verified message. /// </returns> public OutgoingMessage ProcessOutgoing(MessageEnvelope envelope) { if (envelope == null) { throw new ArgumentNullException("envelope"); } OutgoingMessage message = new OutgoingMessage(envelope); return this.ProcessOutgoing(message); }
protected virtual MessageEnvelope ProcessOutgoing(ISmtpMessage message, MessageEnvelope envelope) { OutgoingMessage outgoing = new OutgoingMessage(envelope); if (envelope.Message.IsMDN()) { outgoing.IsMDN = true; outgoing.UseIncomingTrustAnchors = this.Settings.Notifications.UseIncomingTrustAnchorsToSend; } if (envelope.Message.IsDSN()) { outgoing.IsDSN = true; outgoing.UseIncomingTrustAnchors = this.Settings.Notifications.UseIncomingTrustAnchorsToSend; } if (envelope.Message.IsTimelyAndReliable()) { outgoing.IsTimelyAndReliable = true; } outgoing.UsingDeliveryStatus = outgoing.ShouldDeliverFailedStatus(Settings.Notifications); envelope = this.SecurityAgent.ProcessOutgoing(outgoing); Logger.Debug("ProcessedOutgoing"); return envelope; }
// If the message contains trusted internal recipients, drop a copy in the pickup folder, so the message can sent back // through the incoming pipeline protected void RelayInternal(ISmtpMessage message, MessageEnvelope envelope) { InternalMessageSettings settings = m_settings.InternalMessage; if (!(settings.EnableRelay && settings.HasPickupFolder)) { return; } if (!envelope.HasDomainRecipients) { // No internal recipients return; } // // We have some trusted domain recipients. Drop a copy of the message into the message pickup folder // It will get passed back through the message processing loop and treated as Incoming // this.CopyMessage(message, settings.PickupFolder); // // Since we've routed the message ourselves, ensure there is no double delivery by removing them from // the recipient list // envelope.Recipients.Remove(envelope.DomainRecipients); }