Example #1
0
        public void TestIncomingBasic()
        {
            Message message = LoadMessage("simple.eml");
            DirectAddressCollection rcpto = new DirectAddressCollection(
                new DirectAddress[] {
                new DirectAddress("*****@*****.**"),
                new DirectAddress("*****@*****.**")
            }
                );

            IncomingMessage incoming = new IncomingMessage(message, rcpto, new DirectAddress("*****@*****.**"));

            Assert.True(incoming.Recipients.Count == 2);

            DirectAddressCollection routingRecipients = incoming.GetRecipientsInRoutingHeaders();

            Assert.True(routingRecipients.Count == 1);

            Assert.False(incoming.AreAddressesInRoutingHeaders(rcpto));
            incoming.Recipients.Remove(x => MimeStandard.Equals(x.Address, "*****@*****.**"));
            Assert.True(incoming.AreAddressesInRoutingHeaders(rcpto));

            message.ToValue = "*****@*****.**";
            incoming        = new IncomingMessage(message, rcpto, new DirectAddress("*****@*****.**"));
            Assert.False(incoming.AreAddressesInRoutingHeaders(rcpto));

            message.ToValue = "*****@*****.**";
            incoming        = new IncomingMessage(message, rcpto, new DirectAddress("*****@*****.**"));
            Assert.False(incoming.AreAddressesInRoutingHeaders(rcpto));
        }
Example #2
0
        public void LineFolding(int characterCount, int expectedLines)
        {
            string source = new string('a', characterCount);

            string foldedText = null;

            Assert.Null(Record.Exception(() => foldedText = Header.LineFoldText(source, MailStandard.MaxCharsInLine)));
            Assert.True(!string.IsNullOrEmpty(foldedText));

            string[] lines = foldedText.Split(new string[] { MailStandard.CRLF }, StringSplitOptions.None);
            Assert.True(lines.Length == expectedLines);
            for (int i = 0; i < lines.Length; ++i)
            {
                string line = lines[i];
                Assert.True(line.Length > 0);
                Assert.True(line.Length <= MailStandard.MaxCharsInLine);
                if (i > 0)
                {
                    Assert.True(MimeStandard.IsWhitespace(line[0]));
                }
                if ((i == lines.Length - 1) && line.Length > 0)
                {
                    Assert.True(line[line.Length - 1] != MimeStandard.LF);
                }
            }
        }
Example #3
0
        public void TestEndToEnd(string messageText)
        {
            CDO.Message message = this.LoadMessage(messageText);

            string originalSubject     = message.Subject;
            string originalContentType = message.GetContentType();

            //
            // Outgoing
            //
            Assert.DoesNotThrow(() => m_handler.ProcessCDOMessage(message));

            message = this.LoadMessage(message); // re-load the message
            base.VerifyOutgoingMessage(message);
            //
            // Incoming
            //
            Assert.DoesNotThrow(() => m_handler.ProcessCDOMessage(message));

            message = this.LoadMessage(message); // re-load the message
            base.VerifyIncomingMessage(message);

            Assert.True(message.Subject.Equals(originalSubject));
            Assert.True(MimeStandard.Equals(message.GetContentType(), originalContentType));

            Message mailMessage = MailParser.ParseMessage(message.GetMessageText());
            string  header      = mailMessage.Headers.GetValue(SmtpAgent.XHeaders.Receivers);

            Assert.DoesNotThrow(() => MailParser.ParseAddressCollection(header));
            MailAddressCollection addresses = MailParser.ParseAddressCollection(header);

            Assert.True(addresses.Count > 0);
        }
Example #4
0
        public void TestFailedDeliveryStatusSerialization()
        {
            Message source = this.CreateSourceMessage();

            DSN        dsnExpected = this.CreateFailedStatusNotification(3);
            DSNMessage dsnMessage  = source.CreateStatusMessage(new MailAddress(Postmaster), dsnExpected);

            Console.WriteLine(dsnMessage);
            var path = Path.GetTempFileName();

            try
            {
                dsnMessage.Save(path);
                Message loadedMessage = Message.Load(File.ReadAllText(path));
                Assert.True(loadedMessage.IsDSN());
                Assert.Equal(dsnMessage.ParsedContentType.MediaType, loadedMessage.ParsedContentType.MediaType);
                Assert.Equal(dsnMessage.SubjectValue, loadedMessage.SubjectValue);
                Assert.True(loadedMessage.HasHeader(MimeStandard.VersionHeader));
                Assert.True(loadedMessage.HasHeader(MailStandard.Headers.Date));
                Assert.True(loadedMessage.Headers.Count(x => (MimeStandard.Equals(x.Name, MimeStandard.VersionHeader))) == 1);
                var dsnActual = DSNParser.Parse(loadedMessage);

                VerifyEqual(dsnExpected, dsnActual);
            }
            finally
            {
                File.Delete(path);
            }
        }
Example #5
0
        public void TestSerialization_Dispatched()
        {
            Disposition expectedDisposition = new Disposition(MDNStandard.NotificationType.Dispatched);

            Message             source              = this.CreateSourceMessage();
            Notification        notification        = this.CreateDispatchedNotification();
            NotificationMessage notificationMessage = source.CreateNotificationMessage(new MailAddress(source.FromValue), notification);

            var path = Path.GetTempFileName();

            try
            {
                notificationMessage.Save(path);
                Message loadedMessage = Message.Load(File.ReadAllText(path));
                Assert.True(loadedMessage.IsMDN());
                Assert.Equal(notificationMessage.ParsedContentType.MediaType, loadedMessage.ParsedContentType.MediaType);
                Assert.Equal(notificationMessage.SubjectValue, loadedMessage.SubjectValue);
                Assert.True(loadedMessage.HasHeader(MimeStandard.VersionHeader));
                Assert.True(loadedMessage.HasHeader(MailStandard.Headers.Date));
                Assert.True(loadedMessage.Headers.Count(x => (MimeStandard.Equals(x.Name, MimeStandard.VersionHeader))) == 1);
                var mdn = MDNParser.Parse(loadedMessage);
                VerifyEqual(expectedDisposition, mdn.Disposition);
            }
            finally
            {
                File.Delete(path);
            }
        }
Example #6
0
        /// <summary>
        /// Deserialize the field value of a Document-Source field
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static DocumentSource Deserialize(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("text");
            }
            string name  = null;
            string value = null;

            foreach (KeyValuePair <string, string> fieldParam in MimeFieldParameters.Read(text))
            {
                if (MimeStandard.Equals(fieldParam.Key, ParameterNames.Name))
                {
                    name = fieldParam.Value;
                }
                else if (MimeStandard.Equals(fieldParam.Key, ParameterNames.Source))
                {
                    value = fieldParam.Value;
                }
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Invalid Document Source");
            }

            return(new DocumentSource(name, value));
        }
Example #7
0
        public void TestDSNDeserialization()
        {
            string dsnMessage =
                @"MIME-Version:1.0
To:[email protected]
From:[email protected]
Content-Type:multipart/report; boundary=8bacf4b73bef45f2a4f481c20c00e664; report-type=delivery-status
Message-ID:d6bff591-598c-4b20-ac6a-25da27f4918c
Subject:Rejected:Sound Grenade
Date:31 Oct 2012 14:00:52 -07:00


--8bacf4b73bef45f2a4f481c20c00e664
Content-Type:text/plain

Delivery Status Notification
--8bacf4b73bef45f2a4f481c20c00e664
Content-Type:message/delivery-status

Reporting-MTA: dns;reporting_mta_name
X-Original-Message-ID: Message In a Bottle

Final-Recipient: rfc822;[email protected]
Action: failed
Status: 5.0.0

Original-Recipient: rfc822;[email protected]
Final-Recipient: rfc822;[email protected]
Action: failed
Status: 5.0.0 (permanent failure)
Diagnostic-Code: smtp;  550 '*****@*****.**' is not a 
 registered gateway user
Remote-MTA: dns; vnet.ibm.com

Final-Recipient: rfc822;[email protected]
Action: failed
Status: 5.0.0

--8bacf4b73bef45f2a4f481c20c00e664--

";
            Message loadedMessage = Message.Load(dsnMessage);

            Console.WriteLine(loadedMessage);

            Assert.True(loadedMessage.IsDSN());
            Assert.Equal(loadedMessage.ParsedContentType.MediaType, loadedMessage.ParsedContentType.MediaType);
            Assert.Equal(loadedMessage.SubjectValue, loadedMessage.SubjectValue);
            Assert.True(loadedMessage.HasHeader(MimeStandard.VersionHeader));
            Assert.True(loadedMessage.HasHeader(MailStandard.Headers.Date));
            Assert.True(loadedMessage.Headers.Count(x => (MimeStandard.Equals(x.Name, MimeStandard.VersionHeader))) == 1);
            var dsnActual  = DSNParser.Parse(loadedMessage);
            var recipients = dsnActual.PerRecipient.ToList();

            Assert.Equal("smtp;  550 '*****@*****.**' is not a registered gateway user",
                         recipients[1].OtherFields.GetValue(DSNStandard.Fields.DiagnosticCode));
        }
Example #8
0
        TestString MakeRandom(int textLength, double encodeProbability, double lowerCaseProbability)
        {
            Random        rand            = new Random();
            StringBuilder encodedBuilder  = new StringBuilder();
            StringBuilder expectedBuilder = new StringBuilder();
            int           lineLength      = 0;

            for (int i = 0; i < textLength; ++i)
            {
                char ch = (char)rand.Next(1, byte.MaxValue);

                if (ch == MimeStandard.CR || ch == MimeStandard.LF)
                {
                    encodedBuilder.Append(MimeStandard.CRLF);
                    expectedBuilder.Append(MimeStandard.CRLF);
                    lineLength += 2;
                    continue;
                }

                if (ch == QuotedPrintableDecoder.EncodingChar ||
                    MimeStandard.IsWhitespace(ch) ||            // Always encode whitespace
                    rand.NextDouble() <= encodeProbability)
                {
                    if (lineLength > 72)
                    {
                        encodedBuilder.Append("=\r\n"); // Soft line break
                        lineLength = 0;
                    }
                    encodedBuilder.Append(this.Encode(ch, rand.NextDouble() <= lowerCaseProbability));
                    lineLength += 3;
                }
                else
                {
                    if (lineLength >= 75)
                    {
                        encodedBuilder.Append("=\r\n"); // Soft line break
                        lineLength = 0;
                    }
                    encodedBuilder.Append(ch);
                    ++lineLength;
                }
                expectedBuilder.Append(ch);
            }

            return(new TestString
            {
                Encoded = encodedBuilder.ToString(),
                Expected = expectedBuilder.ToString()
            });
        }
        /// <summary>
        /// Locate the index of the matching address
        /// </summary>
        /// <param name="address">address string</param>
        /// <returns>index of the matching DirectAddress object. If not found, returns -1</returns>
        public int IndexOf(string address)
        {
            if (string.IsNullOrEmpty(address))
            {
                throw new ArgumentException("address");
            }

            for (int i = 0, count = this.Count; i < count; ++i)
            {
                if (MimeStandard.Equals(address, this[i].Address))
                {
                    return(i);
                }
            }

            return(-1);
        }
Example #10
0
        public void TestExtensions_ExtensionsSerialization()
        {
            Disposition expectedDisposition = new Disposition(MDNStandard.NotificationType.Processed);

            Message      source       = this.CreateSourceMessage();
            Notification notification = this.CreateProcessedNotification();

            notification.SpecialFields = new HeaderCollection()
            {
                //Header value of empty fails JoinHeader in DefaultSerializer.  Research this...
                //Constructor with StringSegment does not fail.
                new Header(new StringSegment("X-Test1:")),
                new Header(new StringSegment("X-Test2:MyValue"))
            };
            notification.OriginalRecipient = new MailAddress("*****@*****.**");
            NotificationMessage notificationMessage = source.CreateNotificationMessage(new MailAddress(source.FromValue), notification);

            var path = Path.GetTempFileName();

            try
            {
                notificationMessage.Save(path);
                Message loadedMessage = Message.Load(File.ReadAllText(path));
                Assert.True(loadedMessage.IsMDN());
                Assert.Equal(notificationMessage.ParsedContentType.MediaType, loadedMessage.ParsedContentType.MediaType);
                Assert.Equal(notificationMessage.SubjectValue, loadedMessage.SubjectValue);
                Assert.True(loadedMessage.HasHeader(MimeStandard.VersionHeader));
                Assert.True(loadedMessage.HasHeader(MailStandard.Headers.Date));
                Assert.True(loadedMessage.Headers.Count(x => (MimeStandard.Equals(x.Name, MimeStandard.VersionHeader))) == 1);

                var mdn = MDNParser.Parse(loadedMessage);
                VerifyEqual(expectedDisposition, mdn.Disposition);
                Assert.NotNull(mdn.SpecialFields["X-Test1"]);
                Assert.Equal("", mdn.SpecialFields["X-Test1"].ValueRaw);
                Assert.Equal("MyValue", mdn.SpecialFields["X-Test2"].Value);
                Assert.Equal(notification.OriginalRecipient, mdn.OriginalRecipient);
            }
            finally
            {
                File.Delete(path);
            }
        }
Example #11
0
 /// <summary>
 /// Returns true if this object contains the source for the given document name
 /// </summary>
 /// <param name="documentName"></param>
 /// <returns></returns>
 public bool IsSourceForDocument(string documentName)
 {
     return(MimeStandard.Equals(documentName, this.DocumentName));
 }