Beispiel #1
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 SendGridMessage();

            //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.DeliverAsync(message);
        }
        public void Test_EnablingBypassListManagement()
        {
            var mail = BasicMailBuilder
                .EnableBypassListManagement()
                .Build();

            var message = new SendGridMessage();
            message.EnableBypassListManagement();
            Assert.IsFalse(string.IsNullOrEmpty(message.Header.JsonString()));
            Assert.AreEqual(message.Header.JsonString(), mail.Header.JsonString());
        }
Beispiel #3
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 SendGridMessage();

            //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.DeliverAsync(message);
        }
Beispiel #4
0
        /// <summary>
        ///     This feature tags the message with a specific tracking category, which will have aggregated stats
        ///     viewable from your SendGridMessage account page.
        /// </summary>
        public void SetCategory()
        {
            //create a new message object
            var message = new SendGridMessage();

            //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 Categories";

            var category = "vipCustomers";

            message.SetCategory(category);

            //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.DeliverAsync(message);
        }
Beispiel #5
0
        /// <summary>
        ///     This feature wraps an HTML template around your email content.
        ///     This can be useful for sending out newsletters and/or other HTML formatted messages.
        ///     hhttp://docs.sendgrid.com/documentation/apps/email-templates/
        /// </summary>
        public void EnableBypassListManagementEmail()
        {
            //create a new message object
            var message = new SendGridMessage();

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

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

            //set the message body
            var timestamp = DateTime.Now.ToString("HH:mm:ss tt");
            message.Html = "<p style='color:red';>Hello World</p>";
            message.Html += "<p>Sent At : " + timestamp + "</p>";

            message.Text = "Hello World plain text";

            //set the message subject
            message.Subject = "Hello World Bypass List Management Test";

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

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

            //send the mail
            transportInstance.DeliverAsync(message);
        }
        public static void SendEmailViaSendGrid(string to, string from, string subject, string htmlBody, MailType type, string textBody, string[] multipleTo = null)
        {
            try
            {
                //var message = SendGrid.GenerateInstance();
                var message = new SendGridMessage();
                if (String.IsNullOrEmpty(to))
                    message.AddTo(multipleTo);
                else
                    message.AddTo(to);

                //if (multipleTo != null)
                //    message.AddTo(multipleTo);
                //else
                //    message.AddTo(to);

                message.From = new System.Net.Mail.MailAddress(from);
                message.Subject = subject;
                if (type == MailType.TextOnly)
                    message.Text = textBody.Replace(@"\r\n", Environment.NewLine);
                else if (type == MailType.HtmlOnly)
                    message.Html = htmlBody;
                else
                {
                    message.Html = htmlBody;
                    message.Text = textBody;
                }

                //Dictionary<string, string> collection = new Dictionary<string, string>();
                //collection.Add("header", "header");
                //message.Headers = collection;

                message.EnableOpenTracking();
                message.EnableClickTracking();
                message.DisableUnsubscribe();
                message.DisableFooter();
                message.EnableBypassListManagement();
                //var transportInstance = SMTP.GenerateInstance(new System.Net.NetworkCredential(SendGridUsername, SendGridPassword), SendGridSmtpHost, SendGridSmtpPort);
                var transportInstance = new Web(new System.Net.NetworkCredential(SendGridUsername, SendGridPassword));
                transportInstance.Deliver(message);
                if (String.IsNullOrEmpty(to))
                    Console.WriteLine("SendGrid: Email was sent successfully to " + multipleTo);
                else
                    Console.WriteLine("SendGrid: Email was sent successfully to " + to);
            }
            catch (Exception)
            {
                if (String.IsNullOrEmpty(to))
                    Console.WriteLine("SendGrid: Unable to send email to " + multipleTo);
                else
                    Console.WriteLine("SendGrid: Unable to send email to " + to);
                throw;
            }
        }
Beispiel #7
0
 public MailBuilder EnableBypassListManagement()
 {
     sendgrid.EnableBypassListManagement();
     return(this);
 }
        public void EnableBypassListManagement()
        {
            var header = new Header();
            var sendgrid = new SendGridMessage(header);

            sendgrid.EnableBypassListManagement();

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