Exemplo n.º 1
0
        /// <summary>
        /// send a message
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static void Send(MessageSend message)
        {
            SmtpClient smtpServer = new SmtpClient();

            MailMessage mailMessage = PrepareMessage(message);

            if (smtpServer != null)
            {
                smtpServer.Send(mailMessage);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// send a message asynchronously
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static Task SendAsync(MessageSend message)
        {
            SmtpClient smtpServer = new SmtpClient();

            MailMessage mailMessage = PrepareMessage(message);

            if (smtpServer != null)
            {
                return(smtpServer.SendMailAsync(mailMessage));
            }
            else
            {
                return(Task.FromResult(0));
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get a MailMessage Object to sender
        /// </summary>
        /// <param name="message">message</param>
        /// <returns>MailMessage</returns>
        private static MailMessage PrepareMessage(MessageSend message)
        {
            MailMessage mail = new MailMessage();

            mail.Subject    = message.Subject;
            mail.Body       = message.Body;
            mail.IsBodyHtml = true;

            foreach (var mailto in message.MailTo)
            {
                mail.To.Add(new MailAddress(mailto));
            }

            foreach (var ccp in message.CCP)
            {
                mail.CC.Add(new MailAddress(ccp));
            }

            foreach (var cco in message.CCO)
            {
                mail.CC.Add(new MailAddress(cco));
            }

            foreach (var attachment in message.Attachment)
            {
                mail.Attachments.Add(new System.Net.Mail.Attachment(attachment));
            }

            if (message.SubjectEncoding != null)
            {
                mail.SubjectEncoding = message.SubjectEncoding;
            }

            if (message.BodyEnconding != null)
            {
                mail.BodyEncoding = message.BodyEnconding;
            }

            return(mail);
        }
Exemplo n.º 6
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"));
            }
        }