Пример #1
0
        public async Task InboundEmailWithAttachmentsAsync()
        {
            using (var fileStream = File.OpenRead("InboudEmailTestData/email_with_attachments.txt"))
            {
                var parser       = new WebhookParser();
                var inboundEmail = await parser.ParseInboundEmailWebhookAsync(fileStream).ConfigureAwait(false);

                inboundEmail.ShouldNotBeNull();

                inboundEmail.Attachments.ShouldNotBeNull();
                inboundEmail.Attachments.Length.ShouldBe(2);
                inboundEmail.Attachments[0].ContentId.ShouldBeNull();
                inboundEmail.Attachments[0].ContentType.ShouldBe("image/jpeg");
                inboundEmail.Attachments[0].FileName.ShouldBe("001.jpg");
                inboundEmail.Attachments[0].Id.ShouldBe("attachment2");
                inboundEmail.Attachments[0].Name.ShouldBe("001.jpg");
                inboundEmail.Attachments[1].ContentId.ShouldBe("[email protected]");
                inboundEmail.Attachments[1].ContentType.ShouldBe("image/png");
                inboundEmail.Attachments[1].FileName.ShouldBe("image001.png");
                inboundEmail.Attachments[1].Id.ShouldBe("attachment1");
                inboundEmail.Attachments[1].Name.ShouldBe("image001.png");

                inboundEmail.Dkim.ShouldBe("{@hotmail.com : pass}");

                inboundEmail.To[0].Email.ShouldBe("*****@*****.**");
                inboundEmail.To[0].Name.ShouldBe("API test user");

                inboundEmail.Cc.Length.ShouldBe(0);

                inboundEmail.From.Email.ShouldBe("*****@*****.**");
                inboundEmail.From.Name.ShouldBe("Test User");

                inboundEmail.SenderIp.ShouldBe("40.92.19.62");

                inboundEmail.SpamReport.ShouldBeNull();

                inboundEmail.Envelope.From.ShouldBe("*****@*****.**");
                inboundEmail.Envelope.To.Length.ShouldBe(1);
                inboundEmail.Envelope.To.ShouldContain("*****@*****.**");

                inboundEmail.Subject.ShouldBe("Test #1");

                inboundEmail.SpamScore.ShouldBeNull();

                inboundEmail.Charsets.Except(new[]
                {
                    new KeyValuePair <string, Encoding>("to", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("filename", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("html", Encoding.ASCII),
                    new KeyValuePair <string, Encoding>("subject", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("from", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("text", Encoding.ASCII),
                }).Count().ShouldBe(0);

                inboundEmail.Spf.ShouldBe("pass");
            }
        }
Пример #2
0
        public async Task RawPayloadWithAttachmentsAsync()
        {
            var parser = new WebhookParser();

            using (Stream stream = new MemoryStream())
            {
                using (var fileStream = File.OpenRead("InboudEmailTestData/raw_data.txt"))
                {
                    await fileStream.CopyToAsync(stream).ConfigureAwait(false);
                }
                stream.Position = 0;

                InboundEmail inboundEmail = await parser.ParseInboundEmailWebhookAsync(stream).ConfigureAwait(false);

                inboundEmail.ShouldNotBeNull();

                inboundEmail.Dkim.ShouldBe("{@sendgrid.com : pass}");

                var rawEmailTestData = File.ReadAllText("InboudEmailTestData/raw_email.txt");
                inboundEmail.RawEmail.Trim().ShouldBe(rawEmailTestData);

                inboundEmail.To[0].Email.ShouldBe("*****@*****.**");
                inboundEmail.To[0].Name.ShouldBe(string.Empty);

                inboundEmail.Cc.Length.ShouldBe(0);

                inboundEmail.From.Email.ShouldBe("*****@*****.**");
                inboundEmail.From.Name.ShouldBe("Example User");

                inboundEmail.SenderIp.ShouldBe("0.0.0.0");

                inboundEmail.SpamReport.ShouldBeNull();

                inboundEmail.Envelope.From.ShouldBe("*****@*****.**");
                inboundEmail.Envelope.To.Length.ShouldBe(1);
                inboundEmail.Envelope.To.ShouldContain("*****@*****.**");

                inboundEmail.Subject.ShouldBe("Raw Payload");

                inboundEmail.SpamScore.ShouldBeNull();

                inboundEmail.Charsets.Except(new[]
                {
                    new KeyValuePair <string, Encoding>("to", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("subject", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("from", Encoding.UTF8)
                }).Count().ShouldBe(0);

                inboundEmail.Spf.ShouldBe("pass");
            }
        }
Пример #3
0
        public async Task InboundEmail_with_unusual_encoding()
        {
            // Arrange
            var parser = new WebhookParser();

            using (var stream = GetStream(INBOUND_EMAIL_UNUSUAL_ENCODING_WEBHOOK))
            {
                // Act
                var inboundEmail = await parser.ParseInboundEmailWebhookAsync(stream).ConfigureAwait(false);

                // Assert
                inboundEmail.Charsets.ShouldNotBeNull();
                inboundEmail.Charsets.Except(new[]
                {
                    new KeyValuePair <string, Encoding>("to", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("subject", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("from", Encoding.UTF8),
                    new KeyValuePair <string, Encoding>("html", Encoding.ASCII),
                    new KeyValuePair <string, Encoding>("text", Encoding.UTF8)                          // The original encoding is iso-8859-10 but we fallback on UTF-8
                }).Count().ShouldBe(0);
                inboundEmail.Text.Replace("\r\n", "\n").ShouldBe("Hello SendGrid!\n");
            }
        }
Пример #4
0
        public async Task InboundEmailAsync()
        {
            // Arrange
            var parser = new WebhookParser();

            using (var stream = GetStream(INBOUND_EMAIL_WEBHOOK))
            {
                // Act
                var inboundEmail = await parser.ParseInboundEmailWebhookAsync(stream).ConfigureAwait(false);

                // Assert
                inboundEmail.Attachments.ShouldNotBeNull();
                inboundEmail.Attachments.Length.ShouldBe(0);
                inboundEmail.Cc.ShouldNotBeNull();
                inboundEmail.Cc.Length.ShouldBe(0);
                inboundEmail.Charsets.ShouldNotBeNull();
                inboundEmail.Charsets.Length.ShouldBe(5);
                inboundEmail.Dkim.ShouldBe("{@hotmail.com : pass}");
                inboundEmail.From.ShouldNotBeNull();
                inboundEmail.From.Email.ShouldBe("*****@*****.**");
                inboundEmail.From.Name.ShouldBe("Bob Smith");
                inboundEmail.Headers.ShouldNotBeNull();
                inboundEmail.Headers.Length.ShouldBe(40);
                inboundEmail.Html.ShouldStartWith("<html", Case.Insensitive);
                inboundEmail.SenderIp.ShouldBe("10.43.24.23");
                inboundEmail.SpamReport.ShouldBeNull();
                inboundEmail.SpamScore.ShouldBeNull();
                inboundEmail.Spf.ShouldBe("softfail");
                inboundEmail.Subject.ShouldBe("Test #1");
                inboundEmail.Text.Replace("\r\n", "\n").ShouldBe("Test #1\n");
                inboundEmail.To.ShouldNotBeNull();
                inboundEmail.To.Length.ShouldBe(1);
                inboundEmail.To[0].Email.ShouldBe("*****@*****.**");
                inboundEmail.To[0].Name.ShouldBe("Test Recipient");
            }
        }