Пример #1
0
        public static bool TryBindMessage(long attachmentsLength, ref ExchangeItem emailMessage, out bool error)
        {
            error = false;

            try
            {
                if (Message == null)
                {
                    error = true;

                    return(true);
                }

                if (NewMessage)
                {
                    emailMessage = Message;
                    return(true);
                }

                if (!FindItem(Message.Id))
                {
                    error = true;
                }

                emailMessage = Message;
            }
            catch (Exception ex)
            {
                error        = true;
                emailMessage = Message;
            }

            return(true);
        }
Пример #2
0
        private void ExportFilesOnCreated(List <FileInformation> files)
        {
            try
            {
                if (files == null || files.Count == 0)
                {
                    return;
                }

                semaphore.WaitOne();

                ExchangeServiceProvider.SetInProgress(true);

                if (ExchangeServiceProvider.Message == null)
                {
                    ExchangeServiceProvider.CreateMessage();

                    if (ExchangeServiceProvider.Message == null)
                    {
                        files.ForEach(file =>
                        {
                            try
                            {
                                File.Move(file.TempPath, file.FullName);
                            }
                            catch (Exception ex)
                            { }
                        });

                        semaphore.Release();

                        ExchangeServiceProvider.SetInProgress(false);

                        return;
                    }
                }

                var attachmentsSize = files.Sum(p => p.Content.Length);

                System.Threading.Tasks.Task.Run(() =>
                {
                    var attached = false;

                    ExchangeItem message = null;

                    while (!attached)
                    {
                        try
                        {
                            if (LockExist() || !ExchangeServiceProvider.CookieExist())
                            {
                                continue;
                            }

                            var error = false;

                            if (ExchangeServiceProvider.TryBindMessage(attachmentsSize, ref message, out error))
                            {
                                try
                                {
                                    if (error)
                                    {
                                        CreateLockFile(GetMd5Hash(message.Id));

                                        continue;
                                    }

                                    var tempFiles = files.ToList();

                                    for (var i = 0; i < tempFiles.Count; i++)
                                    {
                                        var file = tempFiles[i];

                                        ExchangeServiceProvider.CreateAttachment(file);

                                        files.Remove(file);

                                        try
                                        {
                                            File.Delete(file.TempPath);
                                        }
                                        catch (Exception)
                                        { }
                                    }

                                    attached = !files.Any();

                                    ExchangeServiceProvider.NewMessage = false;
                                }
                                catch (ServiceResponseException ex)
                                {
                                    if (ex != null && (ex.ErrorCode == ServiceError.ErrorItemNotFound || ex.ErrorCode == ServiceError.ErrorMessageSizeExceeded))
                                    {
                                        CreateLockFile(GetMd5Hash(message.Id));
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        { }
                    }

                    ExchangeServiceProvider.SetInProgress(false);

                    if (!semaphore.SafeWaitHandle.IsClosed)
                    {
                        semaphore.Release();
                    }
                });
            }
            catch (Exception ex)
            { }
        }