コード例 #1
0
        private string GetAutoreplyEmailInTo(MailBox account, MailMessage messageItem)
        {
            var autoreplyAddress = GetAddressInList(account, messageItem.ToList) ??
                                   GetAddressInList(account, messageItem.CcList);

            return(autoreplyAddress);
        }
コード例 #2
0
        private MailMessage CreateAutoreply(MailBox account, MailMessage messageItem, string autoreplyEmail)
        {
            var mailMessage   = new MailMessage();
            var stringBuilder = new StringBuilder(account.MailAutoreply.Subject);

            if (!string.IsNullOrEmpty(account.MailAutoreply.Subject))
            {
                stringBuilder.Append(" ");
            }

            mailMessage.Subject       = stringBuilder.AppendFormat("Re: {0}", messageItem.Subject).ToString();
            mailMessage.HtmlBody      = account.MailAutoreply.Html;
            mailMessage.MimeReplyToId = messageItem.MimeMessageId;
            mailMessage.To            = messageItem.From;
            mailMessage.From          = autoreplyEmail ?? account.EMail.ToString();

            if (account.MailSignature == null)
            {
                account.MailSignature = GetMailboxSignature(account.MailBoxId, account.UserId, account.TenantId);
            }

            if (account.MailSignature.IsActive)
            {
                mailMessage.HtmlBody = new StringBuilder(mailMessage.HtmlBody).AppendFormat(
                    @"<div class='tlmail_signature' mailbox_id='{0}'
                         style='font-family:open sans,sans-serif; font-size:12px; margin:0px;'>
                         <div>{1}</div>
                      </div>",
                    account.MailBoxId, account.MailSignature.Html).ToString();
            }
            return(mailMessage);
        }
コード例 #3
0
        private bool HasGroupsInTo(MailBox account, MailMessage messageItem)
        {
            if (!account.IsTeamlab)
            {
                return(false);
            }

            if (account.Groups == null)
            {
                account.Groups = GetGroups(account.MailBoxId);
            }

            foreach (var group in account.Groups)
            {
                if (messageItem.ToList.Any(
                        address =>
                        string.Equals(address.Address, @group.Email, StringComparison.InvariantCultureIgnoreCase)))
                {
                    return(true);
                }

                if (messageItem.CcList.Any(
                        address =>
                        string.Equals(address.Address, @group.Email, StringComparison.InvariantCultureIgnoreCase)))
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #4
0
 public void SaveAutoreplyHistory(MailBox account, MailMessage messageItem)
 {
     using (var db = GetDb())
     {
         var autoreplyDal = new AutoreplyDal(db);
         autoreplyDal.UpdateOrCreateAutoreplyHistory(account.MailBoxId, account.TenantId,
                                                     new MailAddress(messageItem.To).Address);
     }
 }
コード例 #5
0
        private bool IsMessageMassSending(MailMessage message)
        {
            var isMassSending =
                (message.HeaderFieldNames != null &&
                 _listHeaderNames.Any(h =>
                                      message.HeaderFieldNames.AllKeys.FirstOrDefault(
                                          k => k.Equals(h, StringComparison.OrdinalIgnoreCase)) != null)) ||
                (!string.IsNullOrEmpty(message.From) &&
                 _listFromTextToSkip.Any(f =>
                                         message.From.IndexOf(f, StringComparison.OrdinalIgnoreCase) != -1)) ||
                MailUtil.HasUnsubscribeLink(message.HtmlBody);

            return(isMassSending);
        }
コード例 #6
0
        public void SendAutoreply(MailBox account, MailMessage messageItem, string httpContextScheme, ILogger log)
        {
            try
            {
                if (messageItem.Folder != MailFolder.Ids.inbox || account.MailAutoreply == null ||
                    !account.MailAutoreply.TurnOn)
                {
                    return;
                }

                var utcNow = DateTime.UtcNow.Date;

                if (account.MailAutoreply.TurnOnToDate &&
                    account.MailAutoreply.ToDate != DateTime.MinValue &&
                    utcNow > account.MailAutoreply.ToDate)
                {
                    account.MailAutoreply.TurnOn = false;
                    UpdateOrCreateMailboxAutoreply(account.MailBoxId, account.TenantId, account.MailAutoreply);
                    return;
                }

                if (account.MailAutoreply.FromDate <= utcNow &&
                    account.MailAutoreply.FromDate <= messageItem.Date &&
                    !IsMessageMassSending(messageItem) &&
                    !IsCurrentMailboxInFrom(account, messageItem))
                {
                    var apiHelper = new ApiHelper(httpContextScheme);

                    var autoreplyEmail = GetAutoreplyEmailInTo(account, messageItem);
                    if ((autoreplyEmail != null || !HasGroupsInTo(account, messageItem)) &&
                        !HasMailboxAutoreplyHistory(account, messageItem.FromEmail) &&
                        (!account.MailAutoreply.OnlyContacts || apiHelper.SearchEmails(messageItem.FromEmail).Count != 0))
                    {
                        apiHelper.SendMessage(CreateAutoreply(account, messageItem, autoreplyEmail), true);
                        account.MailAutoreplyHistory.Add(messageItem.FromEmail);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(
                    "SendAutoreply Error: {0}, innerException: {1}, account.MailBoxId = {2}, " +
                    "account.UserId = {3}, account.TenantId = {4}",
                    ex, ex.InnerException != null ? ex.InnerException.ToString() : String.Empty,
                    account.MailBoxId, account.UserId, account.TenantId);
            }
        }
コード例 #7
0
        public MailMessage GetMessageTemplate()
        {
            var sendTemplate = new MailMessage
            {
                Attachments   = new List <MailAttachment>(),
                Bcc           = "",
                Cc            = "",
                Subject       = "",
                From          = "",
                HtmlBody      = "",
                Important     = false,
                ReplyTo       = "",
                MimeMessageId = "",
                MimeReplyToId = "",
                To            = "",
                StreamId      = MailUtil.CreateStreamId()
            };

            return(sendTemplate);
        }
コード例 #8
0
        private bool IsCurrentMailboxInFrom(MailBox account, MailMessage messageItem)
        {
            if (string.Equals(messageItem.FromEmail, account.EMail.Address, StringComparison.InvariantCultureIgnoreCase))
            {
                return(true);
            }

            if (!account.IsTeamlab)
            {
                return(false);
            }

            if (account.Aliases == null)
            {
                account.Aliases = GetAliases(account.MailBoxId);
            }
            return
                (account.Aliases.Any(
                     alias =>
                     string.Equals(messageItem.FromEmail, alias.Email, StringComparison.InvariantCultureIgnoreCase)));
        }
コード例 #9
0
        public int CreateSampleMessage(int?folderId, int?mailboxId)
        {
            if (!folderId.HasValue)
            {
                folderId = MailFolder.Ids.inbox;
            }

            if (folderId < MailFolder.Ids.inbox || folderId > MailFolder.Ids.spam)
            {
                throw new ArgumentException(@"Invalid folder id", "folderId");
            }

            if (mailboxId < 0)
            {
                throw new ArgumentException(@"Invalid mailbox id", "mailboxId");
            }

            var accounts = MailBoxManager.GetAccountInfo(TenantId, Username).ToAddressData();

            var account = mailboxId.HasValue
                ? accounts.FirstOrDefault(a => a.MailboxId == mailboxId)
                : accounts.FirstOrDefault(a => a.IsDefault) ?? accounts.FirstOrDefault();

            if (account == null)
            {
                throw new ArgumentException("Mailbox not found");
            }

            var mbox = MailBoxManager.GetUnremovedMailBox(account.MailboxId);

            if (mbox == null)
            {
                throw new ArgumentException("no such mailbox");
            }

            var mimeMessageId = MailUtil.CreateMessageId();

            var restoreFolderId = folderId.Value == MailFolder.Ids.spam || folderId.Value == MailFolder.Ids.trash
                ? MailFolder.Ids.inbox
                : folderId.Value;

            var sampleMessage = new MailMessage
            {
                Date          = DateTime.UtcNow,
                MimeMessageId = mimeMessageId,
                MimeReplyToId = null,
                ChainId       = mimeMessageId,
                ReplyTo       = "",
                From          = mbox.EMail.ToString(),
                FromEmail     = mbox.EMail.Address,
                ToList        = new List <MailAddress> {
                    mbox.EMail
                },
                To               = mbox.EMail.ToString(),
                CcList           = new List <MailAddress>(),
                Cc               = "",
                Bcc              = "",
                Subject          = LOREM_IPSUM_SUBJECT,
                Important        = false,
                TextBodyOnly     = false,
                Introduction     = LOREM_IPSUM_INTRO,
                Attachments      = new List <MailAttachment>(),
                HtmlBodyStream   = new MemoryStream(),
                Size             = LOREM_IPSUM_BODY.Length,
                MailboxId        = mbox.MailBoxId,
                HeaderFieldNames = new NameValueCollection(),
                HtmlBody         = LOREM_IPSUM_BODY,
                Folder           = folderId.Value,
                RestoreFolderId  = restoreFolderId,
                IsNew            = true,
                StreamId         = MailUtil.CreateStreamId()
            };

            MailBoxManager.StoreMailBody(mbox, sampleMessage);

            var id = MailBoxManager.MailSave(mbox, sampleMessage, 0, folderId.Value, restoreFolderId, "api sample", "", false);

            return(id);
        }
コード例 #10
0
ファイル: ApiHelper.cs プロジェクト: zfzfzf/CommunityServer
        public void SendMessage(MailMessage message, bool isAutoreply = false)
        {
            var request = new RestRequest("mail/messages/send.json", Method.PUT);

            var jObject = new JObject {
                { "id", message.Id }
            };

            if (!string.IsNullOrEmpty(message.From))
            {
                jObject.Add("from", message.From);
            }

            jObject.Add("to", message.To);

            if (!string.IsNullOrEmpty(message.Cc))
            {
                jObject.Add("cc", message.Cc);
            }

            if (!string.IsNullOrEmpty(message.Bcc))
            {
                jObject.Add("bcc", message.Bcc);
            }

            jObject.Add("subject", message.Subject);

            jObject.Add("body", message.HtmlBody);

            jObject.Add("mimeReplyToId", message.MimeReplyToId);

            jObject.Add("importance", message.Important);

            if (message.TagIds != null && message.TagIds.Count != 0)
            {
                jObject.Add("tags", JsonConvert.SerializeObject(message.TagIds));
            }

            if (message.Attachments != null && message.Attachments.Count != 0)
            {
                jObject.Add("attachments", JsonConvert.SerializeObject(message.Attachments));
            }

            if (!string.IsNullOrEmpty(message.CalendarEventIcs))
            {
                jObject.Add("calendarIcs", message.CalendarEventIcs);
            }

            jObject.Add("isAutoreply", isAutoreply);

            request.AddParameter("application/json; charset=utf-8", jObject, ParameterType.RequestBody);

            var response = Execute(request);

            if (response.ResponseStatus == ResponseStatus.Completed &&
                (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK))
            {
                return;
            }

            if (response.ErrorException is ApiHelperException)
            {
                throw response.ErrorException;
            }

            throw new ApiHelperException("Send message to api failed.", response.StatusCode, response.Content);
        }