Example #1
0
        public void Generate()
        {
            string returnPath = ReturnPathManager.GenerateReturnPath("test@remote", 10);

            Assert.AreEqual("return-test=remote-A@localhost", returnPath);
        }
Example #2
0
        /// <summary>
        /// Queues the email for relaying.
        /// </summary>
        private async Task <SmtpServerTransactionAsyncResult> QueueForRelayingAsync()
        {
            // The email is for relaying.
            Guid messageID = Guid.NewGuid();

            // Look for any MTA control headers.
            MessageHeaderCollection headers = MessageManager.GetMessageHeaders(Data);

            // Will not be null if the SendGroupID header was present.
            MessageHeader ipGroupHeader = headers.SingleOrDefault(m => m.Name.Equals(MessageHeaderNames.SendGroupID, StringComparison.OrdinalIgnoreCase));

            // Parameter will hold the MtaIPGroup that will be used to relay this message.
            VirtualMta.VirtualMtaGroup mtaGroup = null;
            int ipGroupID = 0;

            if (ipGroupHeader != null)
            {
                if (int.TryParse(ipGroupHeader.Value, out ipGroupID))
                {
                    mtaGroup = VirtualMta.VirtualMtaManager.GetVirtualMtaGroup(ipGroupID);
                }
            }

            #region Look for a send id, if one doesn't exist create it.
            MessageHeader sendIdHeader   = headers.SingleOrDefault(h => h.Name.Equals(MessageHeaderNames.SendID, StringComparison.OrdinalIgnoreCase));
            int           internalSendId = -1;
            if (sendIdHeader != null)
            {
                Sends.Send sndID = await Sends.SendManager.Instance.GetSendAsync(sendIdHeader.Value);

                if (sndID.SendStatus == SendStatus.Discard)
                {
                    return(SmtpServerTransactionAsyncResult.FailedSendDiscarding);
                }
                internalSendId = sndID.InternalID;
            }
            else
            {
                Sends.Send sndID = await Sends.SendManager.Instance.GetDefaultInternalSendIdAsync();

                if (sndID.SendStatus == SendStatus.Discard)
                {
                    return(SmtpServerTransactionAsyncResult.FailedSendDiscarding);
                }
                internalSendId = sndID.InternalID;
            }
            #endregion

            #region Generate Return Path
            string returnPath = string.Empty;

            // Can only return path to messages with one rcpt to
            if (RcptTo.Count == 1)
            {
                // Need to check to see if the message contains a return path overide domain.
                MessageHeader returnPathDomainOverrideHeader = headers.SingleOrDefault(h => h.Name.Equals(MessageHeaderNames.ReturnPathDomain, StringComparison.OrdinalIgnoreCase));

                if (returnPathDomainOverrideHeader != null &&
                    MtaParameters.LocalDomains.Count(d => d.Hostname.Equals(returnPathDomainOverrideHeader.Value, StringComparison.OrdinalIgnoreCase)) > 0)
                {
                    // The message contained a local domain in the returnpathdomain
                    // header so use it instead of the default.
                    returnPath = ReturnPathManager.GenerateReturnPath(RcptTo[0], internalSendId, returnPathDomainOverrideHeader.Value);
                }
                else
                {
                    // The message didn't specify a return path overide or it didn't
                    // contain a localdomain so use the default.
                    returnPath = ReturnPathManager.GenerateReturnPath(RcptTo[0], internalSendId);
                }

                // Insert the return path header.
                Data = MessageManager.AddHeader(Data, new MessageHeader("Return-Path", "<" + returnPath + ">"));
            }
            else
            {
                // multiple rcpt's so can't have unique return paths, use generic mail from.
                returnPath = MailFrom;
            }
            #endregion

            #region Generate a message ID header
            string msgIDHeaderVal = "<" + messageID.ToString("N") + MailFrom.Substring(MailFrom.LastIndexOf("@")) + ">";

            // If there is already a message header, remove it and add our own. required for feedback loop processing.
            if (headers.Count(h => h.Name.Equals("Message-ID", StringComparison.OrdinalIgnoreCase)) > 0)
            {
                Data = MessageManager.RemoveHeader(Data, "Message-ID");
            }

            // Add the new message-id header.
            Data = MessageManager.AddHeader(Data, new MessageHeader("Message-ID", msgIDHeaderVal));
            #endregion

            // Remove any control headers.
            headers = new MessageHeaderCollection(headers.Where(h => h.Name.StartsWith(MessageHeaderNames.HeaderNamePrefix, StringComparison.OrdinalIgnoreCase)));
            foreach (MessageHeader header in headers)
            {
                Data = MessageManager.RemoveHeader(Data, header.Name);
            }

            // If the MTA group doesn't exist or it's not got any IPs, use the default.
            if (mtaGroup == null ||
                mtaGroup.VirtualMtaCollection.Count == 0)
            {
                ipGroupID = VirtualMta.VirtualMtaManager.GetDefaultVirtualMtaGroup().ID;
            }

            // Attempt to Enqueue the Email for Relaying.
            if (!QueueManager.Instance.Enqueue(messageID, ipGroupID, internalSendId, returnPath, RcptTo.ToArray(), Data))
            {
                return(SmtpServerTransactionAsyncResult.FailedToEnqueue);
            }

            return(SmtpServerTransactionAsyncResult.SuccessMessageQueued);
        }
Example #3
0
        public void Bounce()
        {
            string returnpath = ReturnPathManager.GenerateReturnPath("testing@localhost", 1);

            TestMailbox(MtaParameters.BounceDropFolder, returnpath);
        }