Exemplo n.º 1
0
 private string FormatWithRecipientData(string messageText, Recipient recipient)
 {
     return string.Format(messageText, recipient.Name, recipient.Email);
 }
Exemplo n.º 2
0
        private List<Recipient> GetRecipients()
        {
            List<Recipient> recipients = new List<Recipient>();

            string connectString = ConfigurationManager.AppSettings["DBConnectString"];
            using(OdbcConnection conn = new OdbcConnection(connectString))
            {
                conn.Open();

                string sql = "select id, name, email, valid_email from customers";

                using(OdbcCommand cmd = new OdbcCommand(sql, conn))
                {
                    using(OdbcDataReader reader = cmd.ExecuteReader())
                    {
                        while(reader.Read())
                        {
                            Recipient r = new Recipient(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                            recipients.Add(r);
                        }
                    }
                }
            }

            return recipients;
        }
Exemplo n.º 3
0
        public static Boolean SendEmail(String From, Recipient recipient, String Subject, String Text = null, 
            String HTML = null, String emailReplyTo = null, String returnPath = null)
        {
            if (Text != null && HTML != null)
            {
                String from = From;
                List<String> to = recipient.Email
                    .Replace(", ", ",")
                    .Split(',')
                    .ToList();

                Destination destination = new Destination();
                destination.WithToAddresses(recipient.Email);
                //destination.WithCcAddresses(cc);
                //destination.WithBccAddresses(bcc);

                Content subject = new Content();
                subject.WithCharset("UTF-8");
                subject.WithData(Subject);

                Content html = new Content();
                html.WithCharset("UTF-8");
                html.WithData(HTML);

                Content text = new Content();
                text.WithCharset("UTF-8");
                text.WithData(Text);

                Body body = new Body();
                body.WithHtml(html);
                body.WithText(text);

                Message message = new Message();
                message.WithBody(body);
                message.WithSubject(subject);

                string accessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
                string secretKey = ConfigurationManager.AppSettings["AWSSecretKey"];

                AmazonSimpleEmailService ses = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(accessKey, secretKey);
                SendEmailRequest request = new SendEmailRequest();
                request.WithDestination(destination);
                request.WithMessage(message);
                request.WithSource(from);

                if (emailReplyTo != null)
                {
                    List<String> replyto
                        = emailReplyTo
                        .Replace(", ", ",")
                        .Split(',')
                        .ToList();

                    request.WithReplyToAddresses(replyto);
                }

                if (returnPath != null)
                {
                    request.WithReturnPath(returnPath);
                }

                try
                {
                    SendEmailResponse response = ses.SendEmail(request);
                    SendEmailResult result = response.SendEmailResult;
                    return true;
                }
                catch (Exception ex)
                {
                    recipient.ErrorMessage = ex.Message;
                    return false;
                }
            }

            throw new Exception("Specify Text and/or HTML for the email body!");
        }