Beispiel #1
0
        /// <summary>
        /// send a message with send grid api key async
        /// </summary>
        /// <param name="message"></param>
        public static async Task SendGridApiAsync(string sendGridApiKey, MessageCredential credential, MessageSend message)
        {
            dynamic sg = new SendGridAPIClient(sendGridApiKey);

            var mail = PrepareSendGridMessage(credential, message);

            dynamic response = await sg.client.mail.send.post(requestBody : mail.Get());

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                throw new Exception(string.Format("Error {0}, please check {1}", response.StatusCode, "https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html"));
            }
        }
Beispiel #2
0
        /// <summary>
        /// send a message async
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static void Send(MessageSend message, MessageCredential credential, MessageServer server)
        {
            SmtpClient smtpServer = new SmtpClient(server.Host);

            smtpServer.Port      = server.Port;
            smtpServer.EnableSsl = server.EnableSsl;

            smtpServer.UseDefaultCredentials = true;
            smtpServer.Credentials           = new NetworkCredential(credential.UserName, credential.Password);

            MailMessage mailMessage = PrepareMessage(message);

            mailMessage.From = new MailAddress(credential.UserName, credential.DisplayName);

            if (smtpServer != null)
            {
                smtpServer.Send(mailMessage);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Get a mail object to sendgrid
        /// </summary>
        private static Mail PrepareSendGridMessage(MessageCredential credential, MessageSend message)
        {
            Personalization personalizacion = new Personalization();

            foreach (var mailto in message.MailTo)
            {
                personalizacion.AddTo(new Email(mailto));
            }

            foreach (var ccp in message.CCP)
            {
                personalizacion.AddCc(new Email(ccp));
            }

            foreach (var cco in message.CCO)
            {
                personalizacion.AddBcc(new Email(cco));
            }

            Mail mail = new Mail();

            mail.From = new Email(credential.UserName, credential.DisplayName);

            mail.Subject    = message.Subject;
            mail.CustomArgs = message.Args;

            foreach (var attachment in message.Attachment)
            {
                Byte[] bytes = File.ReadAllBytes(attachment);

                mail.AddAttachment(new SendGrid.Helpers.Mail.Attachment()
                {
                    Filename = Path.GetFileName(attachment), Content = Convert.ToBase64String(bytes)
                });

                bytes = null;
            }

            mail.AddContent(new Content("text/html", message.Body));
            mail.AddPersonalization(personalizacion);

            return(mail);
        }