Inheritance: System.EventArgs
Ejemplo n.º 1
0
 public virtual void SendMail(object sender, EmailToSendArgs e)
 {
 }
Ejemplo n.º 2
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (EmailQueue.Count==0)
                return;

            do
            {
                var key = EmailQueue.Keys.First();
                var message = EmailQueue[key];
                string packagePath = Path.Combine(_watchLocation, key + ".xml");

                string outputLocation = _deliveredLocation;
                try
                {
                    if (OnMailToSend != null)
                    {
                        var args = new EmailToSendArgs() {Message = message};
                        OnMailToSend(this, args);

                        if (args.SendingFailed)
                        {
                            outputLocation = _failedLocation;
                        }
                    }
                }
                catch (Exception exception)
                {
                    outputLocation = _failedLocation;
                    log.Fatal("Failed to send email package " + key.ToString(), exception);
                }

                // create a directory for the package
                string deliveryPath = Path.Combine(outputLocation, key.ToString());
                Directory.CreateDirectory(deliveryPath);

                // copy attachments to delivery folder
                if (message.Attachments != null)
                {
                    foreach (var attachment in message.Attachments)
                        File.Copy(attachment.Path, Path.Combine(deliveryPath, Path.GetFileName(attachment.Path)), true);
                }

                // copy the email package to delivery folder
                lock (lockObject)
                {
                    if (File.Exists(Path.Combine(outputLocation, key.ToString() + ".xml")))
                    {
                        try
                        {
                            File.Delete(Path.Combine(outputLocation, key.ToString() + ".xml"));
                        }  catch (Exception deleteException)
                        {
                            // probably something using the file... abort
                            log.WarnFormat("Could not move email package to output location, probably in use. Will retry later.\nSource: {0}\nLocation:{1}\nMessage:{2}",
                                packagePath,
                                Path.Combine(outputLocation, key.ToString() + ".xml"), deleteException);
                            continue;
                        }
                    }
                    File.Move(packagePath, Path.Combine(outputLocation, key.ToString() + ".xml"));

                    EmailQueue.Remove(key);

                    log.InfoFormat("Processed email package {0}", key);
                }

            } while (EmailQueue.Count > 0 && !worker.CancellationPending);
        }
        public override void SendMail(object sender, EmailToSendArgs e)
        {
            if (nextCanSend > DateTime.Now)
                System.Threading.Thread.Sleep(nextCanSend.Subtract(DateTime.Now));

            using (var ses = Amazon.AWSClientFactory.CreateAmazonSimpleEmailServiceClient(
                _configuration.Amazon.Key,
                _configuration.Amazon.Secret))
            {
                Destination destination = new Destination();
                destination.WithToAddresses(e.Message.To.ToStringArray());

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

                Body body = new Body();
                if (!string.IsNullOrWhiteSpace(e.Message.Html))
                {
                    Content html = new Content();
                    html.WithCharset("UTF-8");
                    html.WithData(e.Message.Html);
                    body.WithHtml(html);
                }

                if (!string.IsNullOrWhiteSpace(e.Message.Text))
                {
                    Content text = new Content();
                    text.WithCharset("UTF-8");
                    text.WithData(e.Message.Text);
                    body.WithText(text);
                }

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

                SendEmailRequest request = new SendEmailRequest();
                request.WithDestination(destination);
                request.WithMessage(message);
                request.WithSource(e.Message.From);

                try
                {
                    Console.WriteLine("sending email from {0}", e.Message.From);
                    SendEmailResponse response = ses.SendEmail(request);

                    SendEmailResult result = response.SendEmailResult;

                    logger.Debug("Email sent.");
                    logger.Debug(String.Format("Message ID: {0}",
                                               result.MessageId));

                    nextCanSend = _lastSend.AddSeconds(_maxSendRate);
                }
                catch (Exception ex)
                {
                    logger.Fatal("failed to send email", ex);

                    e.SendingFailed = true;
                }
            }
            base.SendMail(sender, e);
        }