示例#1
0
        /// <summary>
        ///     This feature adds key value identifiers to be sent back as arguments over the event api for
        ///     various events
        /// </summary>
        public void AddUniqueIdentifiers()
        {
            //create a new message object
            var message = new SendGrid();

            //set the message recipients
            foreach (var recipient in _to)
            {
                message.AddTo(recipient);
            }

            //set the sender
            message.From = new MailAddress(_from);

            //set the message body
            message.Text = "Hello World";

            //set the message subject
            message.Subject = "Testing Unique Identifiers";

            var identifiers = new Dictionary<String, String>();
            identifiers["customer"] = "someone";
            identifiers["location"] = "somewhere";

            message.AddUniqueArgs(identifiers);

            //create an instance of the SMTP transport mechanism
            var transportInstance = new Web(new NetworkCredential(_username, _password));

            //enable bypass list management
            message.EnableBypassListManagement();

            //send the mail
            transportInstance.Deliver(message);
        }
示例#2
0
        public void CreateMimeMessage()
        {
            var message = new SendGrid();
            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);
        }
示例#3
0
        public void DisableBcc()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.DisableBcc();

            String json = header.AsJson();
            Assert.AreEqual("{\"filters\" : {\"bcc\" : {\"settings\" : {\"enable\" : \"0\"}}}}", json);
        }
        public void DisableBypassListManagement()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.DisableBypassListManagement();

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"bypass_list_management\" : {\"settings\" : {\"enable\" : \"0\"}}}}", json);
        }
        public void DisableClickTracking()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.DisableClickTracking();

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"clicktrack\" : {\"settings\" : {\"enable\" : \"0\"}}}}", json);
        }
示例#6
0
        public void Send(SendGrid message)
        {
            //enable Open Tracking
            message.EnableOpenTracking();

            //enable clicktracking
            message.EnableClickTracking(false);

            //enable spamcheck
            // http://docs.sendgrid.com/documentation/apps/spam-checker-filter/
            //message.EnableSpamCheck();

            TransportInstance.Deliver(message);
        }
示例#7
0
        public void TestFetchFormParams()
        {
            // Test Variables
            const string toAddress = "*****@*****.**";
            const string ccAddress = "*****@*****.**";
            const string bccAddress = "*****@*****.**";
            const string fromAddress = "*****@*****.**";
            const string subject = "Test Subject";
            const string textBody = "Test Text Body";
            const string htmlBody = "<p>Test HTML Body</p>";
            const string headerKey = "headerkey";
            var testHeader = new Dictionary<string, string> { { headerKey, "headervalue" } };
            const string categoryName = "Example Category";

            var message = new SendGrid();
            message.AddTo(toAddress);
            message.AddCc(ccAddress);
            message.AddBcc(bccAddress);
            message.From = new MailAddress(fromAddress);
            message.Subject = subject;
            message.Text = textBody;
            message.Html = htmlBody;
            message.AddHeaders(testHeader);
            message.Header.SetCategory(categoryName);

            var webApi = new Web(new NetworkCredential(TestUsername, TestPassword));
            var result = webApi.FetchFormParams(message);
            Assert.True(result.Any(r => r.Key == "api_user" && r.Value == TestUsername));
            Assert.True(result.Any(r => r.Key == "api_key" && r.Value == TestPassword));
            Assert.True(result.Any(r => r.Key == "to[]" && r.Value == toAddress));
            Assert.True(result.Any(r => r.Key == "cc[]" && r.Value == ccAddress));
            Assert.True(result.Any(r => r.Key == "bcc[]" && r.Value == bccAddress));
            Assert.True(result.Any(r => r.Key == "from" && r.Value == fromAddress));
            Assert.True(result.Any(r => r.Key == "subject" && r.Value == subject));
            Assert.True(result.Any(r => r.Key == "text" && r.Value == textBody));
            Assert.True(result.Any(r => r.Key == "html" && r.Value == htmlBody));
            Assert.True(
                result.Any(
                    r => r.Key == "headers" && r.Value == String.Format("{{\"{0}\":\"{1}\"}}", headerKey, testHeader[headerKey])));
            Assert.True(
                result.Any(r => r.Key == "x-smtpapi" && r.Value == String.Format("{{\"category\" : \"{0}\"}}", categoryName)));
        }
        public void TestAddAttachment()
        {
            var foo = new Mock<IHeader>();
            var sg = new SendGrid(foo.Object);

            sg.AddAttachment("pnunit.framework.dll");
            sg.AddAttachment("pnunit.framework.dll");
            //Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via path");
            //Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via path x2");

            sg = new SendGrid(foo.Object);
            //sg.AddAttachment(data);
            //sg.AddAttachment(data);
            //Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via attachment");
            //Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via attachment x2");

            sg = new SendGrid(foo.Object);
            //sg.AddAttachment(data.ContentStream, data.ContentType);
            //sg.AddAttachment(data.ContentStream, data.ContentType);
            //Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via stream");
            //Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via stream x2");
        }
示例#9
0
        // this code is used for the SMTPAPI examples
        private static void Main()
        {
            // Create the email object first, then add the properties.
            var myMessage = new SendGrid();
            myMessage.AddTo("*****@*****.**");
            myMessage.From = new MailAddress("*****@*****.**", "John Smith");
            myMessage.Subject = "Testing the SendGrid Library";
            myMessage.Text = "Hello World!";

            // Create credentials, specifying your user name and password.
            var credentials = new NetworkCredential("username", "password");

            // Create a Web transport for sending email.
            var transportWeb = new Web(credentials);

            // Send the email.
            if (transportWeb != null)
                transportWeb.DeliverAsync(myMessage);

            Console.WriteLine("Done!");
            Console.ReadLine();
        }
        public void TestAddCc()
        {
            var foo = new Mock<IHeader>();

            var sg = new SendGrid(foo.Object);
            sg.AddCc("*****@*****.**");
            Assert.AreEqual("*****@*****.**", sg.Cc.First().ToString(), "Single CC Address");

            sg = new SendGrid(foo.Object);
            var strings = new String[2];
            strings[0] = "*****@*****.**";
            strings[1] = "*****@*****.**";
            sg.AddCc(strings);
            Assert.AreEqual("*****@*****.**", sg.Cc[0].ToString(), "Multiple CC addresses, check first one");
            Assert.AreEqual("*****@*****.**", sg.Cc[1].ToString(), "Multiple CC addresses, check second one");

            sg = new SendGrid(foo.Object);
            var a = new Dictionary<String, String>();
            a.Add("DisplayName", "Eric");
            var datastruct = new Dictionary<string, IDictionary<string, string>> { { "*****@*****.**", a } };
            sg.AddCc(datastruct);
            Assert.AreEqual("Eric", sg.Cc.First().DisplayName, "Single CC address with args");
        }
示例#11
0
        /// <summary>
        ///     This feature allows you to create a message template, and specify different replacement
        ///     strings for each specific recipient
        /// </summary>
        public void AddSubstitutionValues()
        {
            //create a new message object
            var message = new SendGrid();

            //set the message recipients
            foreach (var recipient in _to)
            {
                message.AddTo(recipient);
            }

            //set the sender
            message.From = new MailAddress(_from);

            //set the message body
            message.Text = "Hi %name%! Pleased to meet you!";

            //set the message subject
            message.Subject = "Testing Substitution Values";

            //This replacement key must exist in the message body
            var replacementKey = "%name%";

            //There should be one value for each recipient in the To list
            var substitutionValues = new List<String> {"Mr Foo", "Mrs Raz"};

            message.AddSubstitution(replacementKey, substitutionValues);

            //create an instance of the SMTP transport mechanism
            var transportInstance = new Web(new NetworkCredential(_username, _password));

            //enable bypass list management
            message.EnableBypassListManagement();

            //send the mail
            transportInstance.Deliver(message);
        }
        public void EnableFooter()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            var text = "My Text";
            var html = "<body><p>hello, <% name %></p></body>";
            var escHtml = "<body><p>hello, <% name %><\\/p><\\/body>";

            sendgrid.EnableFooter(text, html);

            var json = header.JsonString();
            Assert.AreEqual(
                "{\"filters\" : {\"footer\" : {\"settings\" : {\"enable\" : \"1\",\"text\\/plain\" : \"" + text +
                "\",\"text\\/html\" : \"" + escHtml + "\"}}}}", json);
        }
        public void EnableBcc()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            var email = "*****@*****.**";
            sendgrid.EnableBcc(email);

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"bcc\" : {\"settings\" : {\"enable\" : \"1\",\"email\" : \"" + email + "\"}}}}",
                json);
        }
        public void DisableGoogleAnalytics()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.DisableGoogleAnalytics();

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"ganalytics\" : {\"settings\" : {\"enable\" : \"0\"}}}}", json);
        }
        public void TestGetRcpts()
        {
            var foo = new Mock<IHeader>();
            var sg = new SendGrid(foo.Object);

            sg.AddTo("*****@*****.**");
            sg.AddCc("*****@*****.**");
            sg.AddBcc("*****@*****.**");
            sg.AddBcc("*****@*****.**");

            var rcpts = sg.GetRecipients();
            Assert.AreEqual("*****@*****.**", rcpts.First(), "getRecipients check To");
            Assert.AreEqual("*****@*****.**", rcpts.Skip(1).First(), "getRecipients check Cc");
            Assert.AreEqual("*****@*****.**", rcpts.Skip(2).First(), "getRecipients check Bcc");
            Assert.AreEqual("*****@*****.**", rcpts.Skip(3).First(), "getRecipients check Bcc x2");
        }
        public void EnableUnsubscribe()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            var text = "<% %>";
            var html = "<% name %>";

            var jsonText = "\"text\\/plain\" : \"" + text + "\"";
            var jsonHtml = "\"text\\/html\" : \"" + html + "\"";

            sendgrid.EnableUnsubscribe(text, html);

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"subscriptiontrack\" : {\"settings\" : {\"enable\" : \"1\"," +
                            jsonText + "," + jsonHtml + "}}}}", json);

            header = new Header();
            sendgrid = new SendGrid(header);

            var replace = "John";
            var jsonReplace = "\"replace\" : \"" + replace + "\"";

            sendgrid.EnableUnsubscribe(replace);

            json = header.JsonString();
            Assert.AreEqual(
                "{\"filters\" : {\"subscriptiontrack\" : {\"settings\" : {\"enable\" : \"1\"," + jsonReplace + "}}}}", json);

            text = "bad";
            html = "<% name %>";
            Assert.Throws<Exception>(() => sendgrid.EnableUnsubscribe(text, html));

            text = "<% %>";
            html = "bad";
            Assert.Throws<Exception>(() => sendgrid.EnableUnsubscribe(text, html));
        }
        public void EnableSpamCheck()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            var score = 5;
            var url = "http://www.example.com";
            sendgrid.EnableSpamCheck(score, url);

            var json = header.JsonString();
            Assert.AreEqual(
                "{\"filters\" : {\"spamcheck\" : {\"settings\" : {\"enable\" : \"1\",\"maxscore\" : \"5\",\"url\" : \"http:\\/\\/www.example.com\"}}}}",
                json);
        }
        public void TestAddSubVal()
        {
            var substitutionStrings = new List<String> {"foo", "bar", "beer"};
            var mock = new Mock<IHeader>();

            var sg = new SendGrid(mock.Object);
            sg.AddSubVal("-name-", substitutionStrings);

            mock.Verify(foo => foo.AddSubVal("-name-", substitutionStrings));
        }
        public void TestAddHeaderTo()
        {
            var mock = new Mock<Header>();
            var sg = new SendGrid(mock.Object);

            var strings = new string[2] {"*****@*****.**", "*****@*****.**"};
            sg.Header.AddTo(strings);
            Assert.AreEqual(strings, sg.Header.To, "check the X-Smtpapi to array");
        }
        public void TestSGHeader()
        {
            var foo = new Mock<IHeader>();
            var sg = new SendGrid(foo.Object);

            sg.Subject = "New Test Subject";
            Assert.AreEqual("New Test Subject", sg.Subject, "Subject set ok");
            sg.Subject = null;
            Assert.AreEqual("New Test Subject", sg.Subject, "null subject does not overide previous subject");
        }
        public void TestSetCategory()
        {
            var cat = "foo";
            var mock = new Mock<IHeader>();

            var sg = new SendGrid(mock.Object);
            sg.SetCategory(cat);

            mock.Verify(foo => foo.SetCategory(cat));
        }
        public void EnableGoogleAnalytics()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            var source = "SomeDomain.com";
            var medium = "Email";
            var term = "keyword1, keyword2, keyword3";
            var content = "PG, PG13";
            var campaign = "my_campaign";

            sendgrid.EnableGoogleAnalytics(source, medium, term, content, campaign);

            var jsonSource = "\"utm_source\" : \"SomeDomain.com\"";
            var jsonMedium = "\"utm_medium\" : \"" + medium + "\"";
            var jsonTerm = "\"utm_term\" : \"" + term + "\"";
            var jsonContent = "\"utm_content\" : \"" + content + "\"";
            var jsonCampaign = "\"utm_campaign\" : \"" + campaign + "\"";

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"ganalytics\" : {\"settings\" : {\"enable\" : \"1\"," +
                            jsonSource + "," + jsonMedium + "," + jsonTerm + "," + jsonContent + "," + jsonCampaign + "}}}}",
                json);
        }
        public void EnableOpenTracking()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.EnableOpenTracking();

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"opentrack\" : {\"settings\" : {\"enable\" : \"1\"}}}}", json);
        }
示例#24
0
        public void EnableGravatar()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.EnableGravatar();

            String json = header.AsJson();
            Assert.AreEqual("{\"filters\" : {\"gravatar\" : {\"settings\" : {\"enable\" : \"1\"}}}}", json);
        }
        public void EnableTemplate()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);
            var html = "<% hadhdhd %>";

            var escHtml = "<% hadhdhd %>";
            sendgrid.EnableTemplate(html);

            var json = header.JsonString();
            Assert.AreEqual(
                "{\"filters\" : {\"template\" : {\"settings\" : {\"enable\" : \"1\",\"text\\/html\" : \"" + escHtml + "\"}}}}", json);

            escHtml = "bad";
            Assert.Throws<Exception>(() => sendgrid.EnableTemplate(escHtml));
        }
示例#26
0
        public void TestDisableOpenTracking()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.DisableOpenTracking();

            String json = header.AsJson();
            Assert.AreEqual("{\"filters\" : {\"opentrack\" : {\"settings\" : {\"enable\" : \"0\"}}}}", json);
        }
        public void TestDisableGravatar()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);

            sendgrid.DisableGravatar();

            var json = header.JsonString();
            Assert.AreEqual("{\"filters\" : {\"gravatar\" : {\"settings\" : {\"enable\" : \"0\"}}}}", json);
        }
示例#28
0
        /// <summary>
        ///     Send a simple Plain Text email
        /// </summary>
        public void SimplePlaintextEmail()
        {
            //create a new message object
            var message = new SendGrid();

            //set the message recipients
            foreach (var recipient in _to)
            {
                message.AddTo(recipient);
            }

            //set the sender
            message.From = new MailAddress(_from);

            //set the message body
            message.Text = "Hello World Plain Text";

            //set the message subject
            message.Subject = "Hello World Plain Text Test";

            //create an instance of the Web transport mechanism
            var transportInstance = new Web(new NetworkCredential(_username, _password));

            //send the mail
            transportInstance.Deliver(message);
        }
示例#29
0
        public void EnableClickTracking()
        {
            var header = new Header();
            var sendgrid = new SendGrid(header);
            bool includePlainText = true;
            sendgrid.EnableClickTracking(includePlainText);

            String json = header.AsJson();
            Assert.AreEqual("{\"filters\" : {\"clicktrack\" : {\"settings\" : {\"enable\" : \"1\",\"enable_text\" : \"1\"}}}}", json);
        }
        public void TestAddUniqueIdentifier()
        {
            var kvp = new Dictionary<String, String> { {"foo", "bar"}, {"beer", "good"} };
            var mock = new Mock<IHeader>();

            var sg = new SendGrid(mock.Object);
            sg.AddUniqueIdentifiers(kvp);

            mock.Verify(foo => foo.AddUniqueIdentifier(kvp));
        }