public void Test_IncludeUniqueArg()
        {
            var mail = BasicMailBuilder
                       .IncludeUniqueArg("key", "value")
                       .Build();

            var message = new SendGridMessage();

            message.AddUniqueArgs(new Dictionary <string, string>()
            {
                { "key", "value" }
            });
            Assert.IsFalse(string.IsNullOrEmpty(message.Header.JsonString()));
            Assert.AreEqual(message.Header.JsonString(), mail.Header.JsonString());
        }
Example #2
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);
        }
Example #3
0
        public async Task <IHttpActionResult> EmailInvoice(EmailInputModel model)
        {
            var fromAddress = ConfigurationManager.AppSettings["EmailFromAddress"];
            var user        = Request.GetOwinContext().Request.User;

            var order = await _repo.Get(model.OrderId);

            var report = new InvoiceReport(order) as SectionReport;

            report.Run();
            var memStream = new MemoryStream();
            var pdfExport = new PdfExport();

            pdfExport.Export(report.Document, memStream);
            memStream.Position = 0;
            var attachments = new Dictionary <string, MemoryStream> {
                { $"Invoice {order.Id.ToString("0000")}.pdf", memStream }
            };

            var mailMessage = new SendGridMessage {
                Subject             = model.Subject,
                From                = new MailAddress(fromAddress, user.Identity.Name),
                Text                = model.Body,
                Html                = model.Body.Replace("\n", "<br>"),
                StreamedAttachments = attachments
            };
            var to = model.Address.Where(a => !string.IsNullOrWhiteSpace(a)).ToList();

            if (to.Any())
            {
                mailMessage.AddTo(to);

                model.Bcc.ToList().ForEach(bcc => {
                    if (!string.IsNullOrWhiteSpace(bcc))
                    {
                        mailMessage.AddBcc(bcc);
                    }
                });
            }
            else
            {
                to = model.Bcc.Where(a => !string.IsNullOrWhiteSpace(a)).ToList();
                mailMessage.AddTo(to);
            }

            var delivery = await _repo.RecordInvoiceEmail(mailMessage, order.Id, user.Identity.Name);

            mailMessage.AddUniqueArgs(new Dictionary <string, string> {
                ["order_id"]    = order.Id.ToString(),
                ["delivery_id"] = delivery.Id.ToString()
            });

            try {
                var apiKey       = ConfigurationManager.AppSettings["SendGridApiKey"];
                var transportWeb = new Web(apiKey);
                await transportWeb.DeliverAsync(mailMessage);
            } catch (InvalidApiRequestException ex) {
                var       errorMessage = string.Join(", ", ex.Errors);
                Exception error        = ex;
                while (error != null)
                {
                    errorMessage += $",{error.Message}";
                    error         = error.InnerException;
                }
                await _repo.LogEmailDeliveryError(delivery.Id, errorMessage);

                throw;
            } catch (Exception ex) {
                var errorMessage = string.Empty;
                var error        = ex;
                while (error != null)
                {
                    errorMessage += $",{error.Message}";
                    error         = error.InnerException;
                }
                await _repo.LogEmailDeliveryError(delivery.Id, errorMessage);

                throw;
            }

            var email = string.Join(";", model.Address);

            if (!string.IsNullOrWhiteSpace(email))
            {
                order.Inquiry.Email = email;
            }
            if (order.InvoiceDate == null)
            {
                order.InvoiceDate = DateTime.Now;
            }
            var edited = await _repo.Edit(order, user.Identity.Name);

            return(Ok(edited));
        }