Пример #1
0
        /// <summary>
        /// Deletes the email.
        /// </summary>
        /// <param name="emailSubject">The email subject.</param>
        /// <param name="dateCreation">The date creation.</param>
        /// <returns></returns>
        public List <EmailSubject> DeleteEmail(string emailSubject, string dateCreation)
        {
            var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            var client  = this.FormsAuthenticationHelper.CurrentClient;
            var subList = new List <EmailSubject>();

            if (client != null)
            {
                service.Credentials = new WebCredentials(
                    client.Email.Split('@')[0],
                    MyCryptoHelper.DecryptStringAES(
                        client.Password,
                        ConfigurationManager.AppSettings["KeyForAESCrypto"]));
                service.AutodiscoverUrl(client.Email);

                foreach (var item in service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue)))
                {
                    var email = (EmailMessage)item;
                    if ((email.Subject == emailSubject) && (email.DateTimeCreated.ToString(CultureInfo.InvariantCulture) == dateCreation))
                    {
                        email.Delete(DeleteMode.MoveToDeletedItems);
                    }
                }
            }

            return(subList);
        }
Пример #2
0
        /// <summary>
        /// News the email.
        /// </summary>
        /// <param name="emailModel">The email model.</param>
        /// <returns></returns>
        public bool NewEmail(Email emailModel)
        {
            var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            var client  = this.FormsAuthenticationHelper.CurrentClient;

            if (client != null)
            {
                service.Credentials = new WebCredentials(
                    client.Email.Split('@')[0],
                    MyCryptoHelper.DecryptStringAES(
                        client.Password,
                        ConfigurationManager.AppSettings["KeyForAESCrypto"]));
                service.AutodiscoverUrl(client.Email);

                try
                {
                    // Create and save a folder associated message in the Inbox.
                    var message = new EmailMessage(service)
                    {
                        Subject = emailModel.Subject,
                        Body    = emailModel.Body
                    };

                    message.ToRecipients.Add(emailModel.EmailOwner);
                    message.SendAndSaveCopy();
                }
                catch
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Gets the mails drafts.
        /// </summary>
        /// <param name="enumCatalog">The enum catalog.</param>
        /// <returns></returns>
        public List <string> GetMailsDrafts(EmailCatalog enumCatalog)
        {
            var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            var client  = this.FormsAuthenticationHelper.CurrentClient;
            var subList = new List <string>();

            if (client != null)
            {
                service.Credentials =
                    new WebCredentials(
                        client.Email.Split('@')[0],
                        MyCryptoHelper.DecryptStringAES(
                            client.Password,
                            ConfigurationManager.AppSettings["KeyForAESCrypto"]));
                service.AutodiscoverUrl(client.Email);

                foreach (var item in service.FindItems(WellKnownFolderName.Drafts, new ItemView(int.MaxValue)))
                {
                    var email = (EmailMessage)item;
                    subList.Add(email.Subject);
                }
            }

            return(subList);
        }
Пример #4
0
        /// <summary>
        /// Logins the specified user email.
        /// </summary>
        /// <param name="userEmail">The user email.</param>
        /// <param name="password">The password.</param>
        /// <param name="isPersistent">if set to <c>true</c> [is persistent].</param>
        /// <returns></returns>
        public bool Login(string userEmail, string password, bool isPersistent)
        {
            // var rep = new Repository<Client>(new MyClientForMSExchangeContainer());
            Client client = this.RepositoryClients.SearchFor(p => p.Email == userEmail).SingleOrDefault();

            if ((client != null)
                &&
                (MyCryptoHelper.DecryptStringAES(client.Password, ConfigurationManager.AppSettings["KeyForAESCrypto"]) == password))
            {
                this.CreateCookie(client, isPersistent);
                return(true);
            }

            return(false);
        }
Пример #5
0
        /// <summary>
        /// Deletes the email by identifier.
        /// </summary>
        /// <param name="id">
        /// The identifier.
        /// </param>
        /// <param name="emailStringCatalog">
        /// The email String Catalog.
        /// </param>
        /// <returns>
        /// </returns>
        public bool DeleteEmailById(string id, string emailStringCatalog)
        {
            EmailCatalog emailCatalog;

            switch (emailStringCatalog)
            {
            case "Inbox":
                emailCatalog = EmailCatalog.Inbox;
                break;

            case "SentItems":
                emailCatalog = EmailCatalog.SentItems;
                break;

            case "DeletedItems":
                emailCatalog = EmailCatalog.DeletedItems;
                break;

            case "Drafts":
                emailCatalog = EmailCatalog.Drafts;
                break;

            default:
                emailCatalog = EmailCatalog.Inbox;
                break;
            }

            var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            var client  = this.FormsAuthenticationHelper.CurrentClient;

            if (client != null)
            {
                service.Credentials = new WebCredentials(
                    client.Email.Split('@')[0],
                    MyCryptoHelper.DecryptStringAES(
                        client.Password,
                        ConfigurationManager.AppSettings["KeyForAESCrypto"]));
                service.AutodiscoverUrl(client.Email);

                try
                {
                    FindItemsResults <Item> findResults;
                    switch (emailCatalog)
                    {
                    case EmailCatalog.Inbox:
                        findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
                        break;

                    case EmailCatalog.SentItems:
                        findResults = service.FindItems(WellKnownFolderName.SentItems, new ItemView(int.MaxValue));
                        break;

                    case EmailCatalog.DeletedItems:
                        findResults = service.FindItems(WellKnownFolderName.DeletedItems, new ItemView(int.MaxValue));
                        break;

                    case EmailCatalog.Drafts:
                        findResults = service.FindItems(WellKnownFolderName.Drafts, new ItemView(int.MaxValue));
                        break;

                    default:
                        findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
                        break;
                    }

                    foreach (var item in findResults)
                    {
                        var email = (EmailMessage)item;
                        if (email.InternetMessageId == id)
                        {
                            if (emailCatalog == EmailCatalog.DeletedItems)
                            {
                                email.Delete(DeleteMode.HardDelete);
                            }
                            else
                            {
                                email.Delete(DeleteMode.MoveToDeletedItems);
                            }

                            var catalogId = this.RepositoryCatalogs.SearchFor(x => x.CatalogName == emailCatalog.ToString())
                                            .Select(x => x.CatalogId)
                                            .SingleOrDefault();

                            var emailForDeleting = this.RepositoryEmailItems.SearchFor(
                                emailDeleting => emailDeleting.InternetMessageId == id &&
                                emailDeleting.CatalogId == catalogId &&
                                emailDeleting.ClientId == client.Id).SingleOrDefault();

                            this.RepositoryEmailItems.Delete(emailForDeleting);
                            this.RepositoryEmailItems.Save();
                        }
                    }
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #6
0
        /// <summary>
        /// Gets the emails.
        /// </summary>
        /// <param name="emailCatalog">
        /// The email Catalog.
        /// </param>
        /// <returns>
        /// </returns>
        public string GetEmails(EmailCatalog emailCatalog)
        {
            var sb      = new StringBuilder();
            var service = new ExchangeService(ExchangeVersion.Exchange2010);
            var client  = this.FormsAuthenticationHelper.CurrentClient;

            if (client != null)
            {
                service.Credentials = new WebCredentials(
                    client.Email.Split('@')[0],
                    MyCryptoHelper.DecryptStringAES(
                        client.Password,
                        ConfigurationManager.AppSettings["KeyForAESCrypto"]));
                service.AutodiscoverUrl(client.Email);

                try
                {
                    FindItemsResults <Item> emails;
                    switch (emailCatalog)
                    {
                    case EmailCatalog.Inbox:

                        emails = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
                        break;

                    case EmailCatalog.SentItems:
                        emails = service.FindItems(WellKnownFolderName.SentItems, new ItemView(int.MaxValue));
                        break;

                    case EmailCatalog.DeletedItems:
                        emails = service.FindItems(WellKnownFolderName.DeletedItems, new ItemView(int.MaxValue));
                        break;

                    case EmailCatalog.Drafts:
                        emails = service.FindItems(WellKnownFolderName.Drafts, new ItemView(int.MaxValue));
                        break;

                    default:
                        emails = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
                        break;
                    }

                    var clientId  = this.FormsAuthenticationHelper.CurrentClient.Id;
                    var catalogId =
                        this.RepositoryCatalogs.SearchFor(x => x.CatalogName == emailCatalog.ToString())
                        .Select(x => x.CatalogId)
                        .SingleOrDefault();

                    var items = service.BindToItems(
                        emails.Select(item => item.Id),
                        new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Subject, ItemSchema.Body));
                    var emailItemCollection =
                        items.Select(
                            item =>
                            new EmailItem
                    {
                        Body              = !string.IsNullOrEmpty(item.Item.Body.ToString()) ? item.Item.Body.ToString() : "!!!Email without body!!!",
                        CreationDate      = item.Item.DateTimeCreated,
                        InternetMessageId =
                            item.Item is EmailMessage && !string.IsNullOrEmpty(((EmailMessage)item.Item).InternetMessageId)
                                        ? ((EmailMessage)item.Item).InternetMessageId
                                        : "InvalidInternetMessageId",
                        Subject   = item.Item.Subject ?? "!!!Email without subject!!!",
                        CatalogId = catalogId,
                        ClientId  = clientId
                    }).ToList();

                    var existsEmails =
                        this.RepositoryEmailItems.SearchFor(x => x.CatalogId == catalogId && x.ClientId == clientId);
                    foreach (var emailItem in emailItemCollection)
                    {
                        // add to db if not exist
                        EmailItem item = emailItem;
                        if (
                            !existsEmails.Any(e => e.InternetMessageId == item.InternetMessageId))
                        {
                            this.RepositoryEmailItems.Add(emailItem);
                            this.RepositoryEmailItems.Save();
                        }
                    }

                    // var allEmailsInCatalog = this.RepositoryClients.GetById(clientId).EmailItems
                    // .Where(emailItem => emailItem.CatalogId == catalogId)
                    // .OrderByDescending(element => element.CreationDate);
                    var allEmailsInCatalog =
                        this.RepositoryEmailItems.GetAll().Where(element => element.CatalogId == catalogId &&
                                                                 element.ClientId == clientId).OrderByDescending(element => element.CreationDate);

                    foreach (var item in allEmailsInCatalog)
                    {
                        var subjectWithId = new SubjectWithId
                        {
                            Subject      = item.Subject,
                            Id           = item.InternetMessageId,
                            DateCreation =
                                item.CreationDate.ToString(
                                    CultureInfo.InvariantCulture)
                        };
                        var jsonUser = JsonConvert.SerializeObject(subjectWithId);
                        sb.Append(jsonUser + ",");
                    }

                    sb.Remove(sb.Length - 1, 1);
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }

            return(string.Format(Constants.FormatJson, sb));
        }