Esempio n. 1
0
 internal void SendBySmtpClient(NotifyMessage m)
 {
     using (var message = BuildMailMessage(m))
     {
         clientSMTP.Send(message);
     }
 }
Esempio n. 2
0
        private MailMessage BuildMailMessage(NotifyMessage message)
        {
            var email = new MailMessage
            {
                BodyEncoding = Encoding.UTF8,
                SubjectEncoding = Encoding.UTF8,
                From = new MailAddress(message.From),
            };

            var recipients = message.To.Split('|');
            foreach (string address in recipients)
            {
                email.To.Add(address);
            }

            email.ReplyTo = message.ReplayTo!=null ? new MailAddress(message.ReplayTo) : email.From;

            email.Subject = message.Subject.Trim(' ', '\t', '\n', '\r');

            if (message.ContentType == "html")
            {
                email.Body = HtmlUtil.GetText(message.Content);
                var html = String.Format(HtmlForm, message.Content);
                var alternate = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, "text/html");
                email.AlternateViews.Add(alternate);
            }
            else
            {
                email.Body = message.Content;
            }

            return email;
        }
        public override SendResponse ProcessMessage(INoticeMessage message)
        {
            try
            {
                var result = SendResult.OK;
                var username = CoreContext.UserManager.GetUsers(new Guid(message.Recipient.ID)).UserName;
                if (string.IsNullOrEmpty(username))
                {
                    result = SendResult.IncorrectRecipient;
                }
                else
                {
                    var m = new NotifyMessage
                    {
                        To = username,
                        Subject = message.Subject,
                        ContentType = message.ContentType,
                        Content = message.Body,
                        Sender = senderName,
                        CreationDate = DateTime.UtcNow,
                    };

                    var tenant = CoreContext.TenantManager.GetCurrentTenant(false);
                    m.Tenant = tenant == null ? Tenant.DEFAULT_TENANT : tenant.TenantId;

                    sender.Send(m);
                }
                return new SendResponse(message, senderName, result);
            }
            catch (Exception ex)
            {
                return new SendResponse(message, senderName, ex);
            }
        }
Esempio n. 4
0
 internal void SendByAmazonSES(NotifyMessage m)
 {
     using (var message = BuildMailMessage(m))
     {
         var awsEmail = new AWSEmail();
         awsEmail.SendEmail(message);
     }
 }
 public NoticeSendResult Send(NotifyMessage m)
 {
     using (var notifyClient = new NotifyServiceClient())
     {
         notifyClient.SendNotifyMessage(m);
     }
     return NoticeSendResult.OK;
 }
Esempio n. 6
0
 public virtual NoticeSendResult Send(NotifyMessage m)
 {
     var smtpClient = new SmtpClient(host, port) { Credentials = credentials, EnableSsl = ssl, };
     var result = NoticeSendResult.TryOnceAgain;
     try
     {
         try
         {
             var mail = BuildMailMessage(m);
             smtpClient.Send(mail);
             result = NoticeSendResult.OK;
         }
         catch (Exception e)
         {
             Log.ErrorFormat("Tenant: {0}, To: {1} - {2}", m.Tenant, m.To, e);
             throw;
         }
     }
     catch (ArgumentException)
     {
         result = NoticeSendResult.MessageIncorrect;
     }
     catch (ObjectDisposedException)
     {
         result = NoticeSendResult.SendingImpossible;
     }
     catch (InvalidOperationException)
     {
         result = string.IsNullOrEmpty(smtpClient.Host) || smtpClient.Port == 0 ? NoticeSendResult.SendingImpossible : NoticeSendResult.TryOnceAgain;
     }
     catch (SmtpFailedRecipientException e)
     {
         if (e.StatusCode == SmtpStatusCode.MailboxBusy ||
             e.StatusCode == SmtpStatusCode.MailboxUnavailable ||
             e.StatusCode == SmtpStatusCode.ExceededStorageAllocation)
         {
             result = NoticeSendResult.TryOnceAgain;
         }
         else if (e.StatusCode == SmtpStatusCode.MailboxNameNotAllowed ||
             e.StatusCode == SmtpStatusCode.UserNotLocalWillForward ||
             e.StatusCode == SmtpStatusCode.UserNotLocalTryAlternatePath)
         {
             result = NoticeSendResult.MessageIncorrect;
         }
         else if (e.StatusCode != SmtpStatusCode.Ok)
         {
             result = NoticeSendResult.TryOnceAgain;
         }
     }
     catch (SmtpException)
     {
         result = NoticeSendResult.SendingImpossible;
     }
     return result;
 }
        private NotifyMessage CreateNotifyMessage(INoticeMessage message)
        {
            var m = new NotifyMessage
            {
                Subject = message.Subject.Trim(' ', '\t', '\n', '\r'),
                ContentType = message.ContentType,
                Content = message.Body,
                Sender = senderName,
                CreationDate = DateTime.UtcNow,
            };

            var tenant = CoreContext.TenantManager.GetCurrentTenant(false);
            m.Tenant = tenant == null ? Tenant.DEFAULT_TENANT : tenant.TenantId;

            var from = MailAddressUtils.Create(CoreContext.Configuration.SmtpSettings.SenderAddress, CoreContext.Configuration.SmtpSettings.SenderDisplayName);
            var fromTag = message.Arguments.FirstOrDefault(x => x.Tag.Equals("MessageFrom"));
            if (fromTag != null && fromTag.Value != null)
            {
                try
                {
                    from = MailAddressUtils.Create(from.Address, fromTag.Value.ToString());
                }
                catch { }
            }
            m.From = from.ToString();

            var to = new List<string>();
            foreach (var address in message.Recipient.Addresses)
            {
                to.Add(MailAddressUtils.Create(address, message.Recipient.Name).ToString());
            }
            m.To = string.Join("|", to.ToArray());

            var replyTag = message.Arguments.FirstOrDefault(x => x.Tag == "replyto");
            if (replyTag != null && replyTag.Value is string)
            {
                try
                {
                    m.ReplyTo = MailAddressUtils.Create((string)replyTag.Value).ToString();
                }
                catch (Exception e)
                {
                    LogManager.GetLogger("ASC.Notify").Error("Error creating reply to tag for: " + replyTag.Value, e);
                }
            }

            var priority = message.Arguments.FirstOrDefault(a => a.Tag == "Priority");
            if (priority != null)
            {
                m.Priority = Convert.ToInt32(priority.Value);
            }

            return m;
        }
 public void SendNotifyMessage(NotifyMessage notifyMessage)
 {
     try
     {
         db.SaveMessage(notifyMessage);
     }
     catch (Exception e)
     {
         log.Error(e);
         throw;
     }
 }
Esempio n. 9
0
        private NotifyMessage CreateNotifyMessage(INoticeMessage message)
        {
            var m = new NotifyMessage
            {
                Subject = message.Subject.Trim(' ', '\t', '\n', '\r'),
                ContentType = message.ContentType,
                Content = message.Body,
                Sender = senderName,
                CreationDate = DateTime.UtcNow,
            };

            var tenant = CoreContext.TenantManager.GetCurrentTenant(false);
            m.Tenant = tenant == null ? Tenant.DEFAULT_TENANT : tenant.TenantId;

            var from = new MailAddress(CoreContext.Configuration.SmtpSettings.SenderAddress, CoreContext.Configuration.SmtpSettings.SenderDisplayName);
            var fromTag = message.Arguments.FirstOrDefault(x => x.Tag.Name.Equals(NotifyArgumentConstants.MessageFrom));
            if (fromTag != null && fromTag.Value != null)
            {
                try
                {
                    from = new MailAddress(from.Address, fromTag.Value.ToString(), Encoding.UTF8);
                }
                catch { }
            }
            m.From = from.ToString();

            var to = new List<string>();
            var nameOne = false;
            foreach (var address in message.Recipient.Addresses)
            {
                var recipient = !nameOne ? new MailAddress(address, message.Recipient.Name) : new MailAddress(address);
                to.Add(recipient.ToString());
                nameOne = true;
            }
            m.To = string.Join("|", to.ToArray());

            var replyTag = message.Arguments.FirstOrDefault(x => x.Tag.Name == "replyto");
            if (replyTag != null && replyTag.Value is string)
            {
                try
                {
                    m.ReplyTo = new MailAddress((string)replyTag.Value).ToString();
                }
                catch (Exception e)
                {
                    LogManager.GetLogger("ASC.Notify").Error("Error creating reply to tag for: " + replyTag.Value, e);
                }
            }

            return m;
        }
 public NoticeSendResult Send(NotifyMessage m)
 {
     try
     {
         using (var notifyClient = new NotifyServiceClient())
         {
             notifyClient.SendNotifyMessage(m);
         }
         return NoticeSendResult.OK;
     }
     catch (ReflectionTypeLoadException)
     {
         using (var notifyClient = new NotifyServiceClient())
         {
             notifyClient.SendNotifyMessage(m);
         }
         return NoticeSendResult.OK;
     }
 }
Esempio n. 11
0
 public NoticeSendResult Send(NotifyMessage m)
 {
     var text = m.Content;
     if (!string.IsNullOrEmpty(text))
     {
         text = text.Replace("\r\n", "\n").Trim('\n', '\r');
         text = Regex.Replace(text, "\n{3,}", "\n\n");
     }
     try
     {
         service.SendMessage(m.Tenant, null, m.To, text, m.Subject);
     }
     catch (Exception e)
     {
         log.ErrorFormat("Unexpected error, {0}, {1}, {2}",
                e.Message, e.StackTrace, e.InnerException != null ? e.InnerException.Message : string.Empty);
     }
     return NoticeSendResult.OK;
 }
Esempio n. 12
0
        public void SaveMessage(NotifyMessage m)
        {
            using (var db = GetDb())
            using (var tx = db.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                var i = new SqlInsert("notify_queue")
                    .InColumns("notify_id", "tenant_id", "sender", "reciever", "subject", "content_type", "content", "sender_type", "creation_date", "reply_to")
                    .Values(0, m.Tenant, m.From, m.To, m.Subject, m.ContentType, m.Content, m.Sender, m.CreationDate, m.ReplyTo)
                    .Identity(0, 0, true);

                var id = db.ExecuteScalar<int>(i);

                i = new SqlInsert("notify_info")
                    .InColumns("notify_id", "state", "attempts", "modify_date", "priority")
                    .Values(id, 0, 0, DateTime.UtcNow, m.Priority);
                db.ExecuteNonQuery(i);

                tx.Commit();
            }
        }
Esempio n. 13
0
        public override NoticeSendResult Send(NotifyMessage m)
        {
            var result = default(NoticeSendResult);
            try
            {
                try
                {
                    result = SendMessage(m);
                }
                catch (Exception e)
                {
                    Log.ErrorFormat("Tenant: {0}, To: {1} - {2}", m.Tenant, m.To, e);
                    throw;
                }
            }
            catch (ArgumentException)
            {
                result = NoticeSendResult.MessageIncorrect;
            }
            catch (MessageRejectedException)
            {
                result = NoticeSendResult.SendingImpossible;
            }
            catch (AmazonSimpleEmailServiceException e)
            {
                result = e.ErrorType == ErrorType.Sender ? NoticeSendResult.MessageIncorrect : NoticeSendResult.TryOnceAgain;
            }
            catch (Exception)
            {
                result = NoticeSendResult.SendingImpossible;
            }

            if (result == NoticeSendResult.MessageIncorrect || result == NoticeSendResult.SendingImpossible)
            {
                log.DebugFormat("Amazon sending failed: {0}, fallback to smtp", result);
                result = base.Send(m);
            }
            return result;
        }
Esempio n. 14
0
 public NoticeSendResult Send(NotifyMessage m)
 {
     service.SendMessage(m.To, m.Subject, m.Content, m.Tenant);
     return NoticeSendResult.OK;
 }
Esempio n. 15
0
        private MailMessage BuildMailMessage(NotifyMessage m)
        {
            var email = new MailMessage
            {
                BodyEncoding = Encoding.UTF8,
                SubjectEncoding = Encoding.UTF8,
                From = MailAddressUtils.Create(m.From),
                Subject = m.Subject,
            };

            foreach (var to in m.To.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
            {
                email.To.Add(MailAddressUtils.Create(to));
            }

            if (m.ContentType == Pattern.HTMLContentType)
            {
                email.Body = HtmlUtil.GetText(m.Content);
                email.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(GetHtmlView(m.Content), Encoding.UTF8, "text/html"));
            }
            else
            {
                email.Body = m.Content;
            }

            if (!string.IsNullOrEmpty(m.ReplyTo))
            {
                email.ReplyToList.Add(MailAddressUtils.Create(m.ReplyTo));
            }

            return email;
        }
 internal static bool SendMail(NotifyMessage m)
 {
     Thread.Sleep(rnd.Next(1000, 3000));
     return (rnd.Next(100)>10);
 }
Esempio n. 17
0
        private NoticeSendResult SendMessage(NotifyMessage m)
        {
            //Check if we need to query stats
            RefreshQuotaIfNeeded();
            if (quota != null)
            {
                lock (locker)
                {
                    if (quota.Max24HourSend <= quota.SentLast24Hours)
                    {
                        //Quota exceeded, queue next refresh to +24 hours
                        lastRefresh = DateTime.UtcNow.AddHours(24);
                        log.WarnFormat("Quota limit reached. setting next check to: {0}", lastRefresh);
                        return NoticeSendResult.SendingImpossible;
                    }
                }
            }

            var dest = new Destination
            {
                ToAddresses = m.To.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(a => MailAddressUtils.Create(a).Address).ToList(),
            };

            var subject = new Content(MimeHeaderUtils.EncodeMime(m.Subject)) { Charset = Encoding.UTF8.WebName, };

            Body body;
            if (m.ContentType == Pattern.HTMLContentType)
            {
                body = new Body(new Content(HtmlUtil.GetText(m.Content)) { Charset = Encoding.UTF8.WebName });
                body.Html = new Content(GetHtmlView(m.Content)) { Charset = Encoding.UTF8.WebName };
            }
            else
            {
                body = new Body(new Content(m.Content) { Charset = Encoding.UTF8.WebName });
            }

            var from = MailAddressUtils.Create(m.From).ToEncodedString();
            var request = new SendEmailRequest(from, dest, new Message(subject, body));
            if (!string.IsNullOrEmpty(m.ReplyTo))
            {
                request.ReplyToAddresses.Add(MailAddressUtils.Create(m.ReplyTo).Address);
            }

            ThrottleIfNeeded();

            var response = ses.SendEmail(request);
            lastSend = DateTime.UtcNow;

            return response != null ? NoticeSendResult.OK : NoticeSendResult.TryOnceAgain;
        }
Esempio n. 18
0
        public virtual NoticeSendResult Send(NotifyMessage m)
        {
            CoreContext.TenantManager.SetCurrentTenant(m.Tenant);
            var smtpClient = GetSmtpClient();
            var result = NoticeSendResult.TryOnceAgain;
            try
            {
                try
                {
                    var mail = BuildMailMessage(m);

                    if (WorkContext.IsMono)
                    {
                        ServicePointManager.ServerCertificateValidationCallback = (s, cert, c, p) => true;
                    }
   
                    smtpClient.Send(mail);
                    result = NoticeSendResult.OK;
                }
                catch (Exception e)
                {
                    Log.ErrorFormat("Tenant: {0}, To: {1} - {2}", m.Tenant, m.To, e);
                    throw;
                }
            }
            catch (ArgumentException)
            {
                result = NoticeSendResult.MessageIncorrect;
            }
            catch (ObjectDisposedException)
            {
                result = NoticeSendResult.SendingImpossible;
            }
            catch (InvalidOperationException)
            {
                result = string.IsNullOrEmpty(smtpClient.Host) || smtpClient.Port == 0 ? NoticeSendResult.SendingImpossible : NoticeSendResult.TryOnceAgain;
            }
            catch (SmtpFailedRecipientException e)
            {
                if (e.StatusCode == SmtpStatusCode.MailboxBusy ||
                    e.StatusCode == SmtpStatusCode.MailboxUnavailable ||
                    e.StatusCode == SmtpStatusCode.ExceededStorageAllocation)
                {
                    result = NoticeSendResult.TryOnceAgain;
                }
                else if (e.StatusCode == SmtpStatusCode.MailboxNameNotAllowed ||
                         e.StatusCode == SmtpStatusCode.UserNotLocalWillForward ||
                         e.StatusCode == SmtpStatusCode.UserNotLocalTryAlternatePath)
                {
                    result = NoticeSendResult.MessageIncorrect;
                }
                else if (e.StatusCode != SmtpStatusCode.Ok)
                {
                    result = NoticeSendResult.TryOnceAgain;
                }
            }
            catch (SmtpException)
            {
                result = NoticeSendResult.SendingImpossible;
            }
            finally
            {
                smtpClient.Dispose();
            }
            return result;
        }
 internal void SendByJabber(NotifyMessage m)
 {
     jsc.SendMessage(m.To, m.Subject, m.Content, m.Tenant);
 }