Exemplo n.º 1
0
        public void CreateMimeMessage()
        {
            var message    = new SendGridMessage();
            var attachment = Path.GetTempFileName();
            var text       = "this is a test";
            var html       = "<b>This<\b> is a better test";
            var headers    = new KeyValuePair <String, String>("custom", "header");

            message.AddAttachment(attachment);
            message.Text = text;
            message.Html = html;
            message.AddTo("*****@*****.**");
            message.From = new MailAddress("*****@*****.**");
            message.AddHeaders(new Dictionary <string, string> {
                { headers.Key, headers.Value }
            });
            message.EnableGravatar();

            var mime = message.CreateMimeMessage();

            var sr     = new StreamReader(mime.AlternateViews[0].ContentStream);
            var result = sr.ReadToEnd();

            Assert.AreEqual(text, result);

            sr     = new StreamReader(mime.AlternateViews[1].ContentStream);
            result = sr.ReadToEnd();
            Assert.AreEqual(html, result);

            result = mime.Headers.Get(headers.Key);
            Assert.AreEqual(headers.Value, result);

            result = mime.Headers.Get("X-Smtpapi");
            var expected = "{\"filters\" : {\"gravatar\" : {\"settings\" : {\"enable\" : \"1\"}}}}";

            Assert.AreEqual(expected, result);

            result = mime.Attachments[0].Name;
            Assert.AreEqual(Path.GetFileName(attachment), result);
        }