public MailAttachment AddCalendarBody(int id_message, string ical_body)
        {
            if (string.IsNullOrEmpty(ical_body))
            {
                throw new ArgumentException(@"Empty calendar body", "ical_body");
            }

            var calendar = MailUtil.ParseValidCalendar(ical_body, _log);

            if (calendar == null)
            {
                throw new ArgumentException(@"Invalid calendar body", "ical_body");
            }

            using (var ms = new MemoryStream())
            {
                using (var writer = new StreamWriter(ms))
                {
                    writer.Write(ical_body);
                    writer.Flush();
                    ms.Position = 0;

                    var attachment = MailBoxManager.AttachFile(TenantId, Username, id_message, calendar.Method.ToLowerInvariant() + ".ics", ms, "text/calendar");
                    return(attachment);
                }
            }
        }
Example #2
0
        private void SaveIcsAttachment(MailDraft draft, MimeMessage mimeMessage)
        {
            if (string.IsNullOrEmpty(draft.CalendarIcs) || mimeMessage.Attachments.Count() != 1)
            {
                return;
            }

            try
            {
                var icsAttachment = mimeMessage.Attachments.First() as TextPart;

                if (icsAttachment == null)
                {
                    return;
                }

                using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(icsAttachment.Text)))
                {
                    manager.AttachFile(draft.Mailbox.TenantId, draft.Mailbox.UserId,
                                       draft.Id, icsAttachment.ContentType.Name, memStream);
                }
            }
            catch (Exception ex)
            {
                log.Warn(string.Format("Problem with attach ICAL to message. mailId={0} Exception:\r\n{1}\r\n", draft.Id, ex));
            }
        }
Example #3
0
        public MailAttachment AppendAttachmentsToSampleMessage(
            int?messageId, string filename, Stream stream, string contentType)
        {
            if (!messageId.HasValue || messageId.Value <= 0)
            {
                throw new ArgumentException(@"Invalid message id", "messageId");
            }

            var message = MailBoxManager.GetMailInfo(TenantId, Username, messageId.Value, new MailMessage.Options());

            if (message == null)
            {
                throw new AttachmentsException(AttachmentsException.Types.MessageNotFound, "Message not found.");
            }

            if (!message.Uidl.Equals(SAMPLE_UIDL))
            {
                throw new Exception("Message is not api sample.");
            }

            if (string.IsNullOrEmpty(filename))
            {
                throw new Exception("File name is empty.");
            }

            if (stream == null)
            {
                throw new Exception("File stream is empty.");
            }

            contentType = string.IsNullOrEmpty(contentType) ? MimeMapping.GetMimeMapping(filename) : contentType;

            return(MailBoxManager.AttachFile(TenantId, Username, message, filename, stream, contentType));
        }
        public MailAttachment AddAttachment(int id_message, string name, Stream file, string content_type)
        {
            var attachment = MailBoxManager.AttachFile(TenantId, Username, id_message, name, file, content_type);

            return(attachment);
        }
Example #5
0
        public override FileUploadResult ProcessUpload(HttpContext context)
        {
            var            fileName   = string.Empty;
            MailAttachment attachment = null;

            try
            {
                if (FileToUpload.HasFilesToUpload(context))
                {
                    try
                    {
                        Thread.CurrentThread.CurrentCulture   = CurrentCulture;
                        Thread.CurrentThread.CurrentUICulture = CurrentCulture;

                        var mailId   = Convert.ToInt32(context.Request["messageId"]);
                        var copyToMy = Convert.ToInt32(context.Request["copyToMy"]);

                        if (mailId < 1)
                        {
                            throw new AttachmentsException(AttachmentsException.Types.MessageNotFound, "Message not yet saved!");
                        }

                        var item = MailBoxManager.GetMailInfo(TenantId, Username, mailId, false, false);
                        if (item == null)
                        {
                            throw new AttachmentsException(AttachmentsException.Types.MessageNotFound, "Message not found.");
                        }

                        if (string.IsNullOrEmpty(item.StreamId))
                        {
                            throw new AttachmentsException(AttachmentsException.Types.BadParams, "Have no stream");
                        }

                        var postedFile = new FileToUpload(context);
                        fileName = context.Request["name"];

                        if (copyToMy == 1)
                        {
                            var uploadedFile = FileUploader.Exec(Global.FolderMy.ToString(), fileName, postedFile.ContentLength, postedFile.InputStream, true);
                            return(new FileUploadResult
                            {
                                Success = true,
                                FileName = uploadedFile.Title,
                                FileURL = FilesLinkUtility.GetFileWebPreviewUrl(uploadedFile.Title, uploadedFile.ID),
                                Data = new MailAttachment
                                {
                                    fileId = Convert.ToInt32(uploadedFile.ID),
                                    fileName = uploadedFile.Title,
                                    size = uploadedFile.ContentLength,
                                    contentType = uploadedFile.ConvertedType,
                                    attachedAsLink = true,
                                    tenant = TenantId,
                                    user = Username
                                }
                            });
                        }

                        attachment = new MailAttachment
                        {
                            fileId   = -1,
                            size     = postedFile.ContentLength,
                            fileName = fileName,
                            streamId = item.StreamId,
                            tenant   = TenantId,
                            user     = Username
                        };

                        attachment = MailBoxManager.AttachFile(TenantId, Username, mailId, fileName, postedFile.InputStream);

                        return(new FileUploadResult
                        {
                            Success = true,
                            FileName = attachment.fileName,
                            FileURL = attachment.storedFileUrl,
                            Data = attachment
                        });
                    }
                    catch (AttachmentsException e)
                    {
                        string errorMessage;

                        switch (e.ErrorType)
                        {
                        case AttachmentsException.Types.BadParams:
                            errorMessage = MailScriptResource.AttachmentsBadInputParamsError;
                            break;

                        case AttachmentsException.Types.EmptyFile:
                            errorMessage = MailScriptResource.AttachmentsEmptyFileNotSupportedError;
                            break;

                        case AttachmentsException.Types.MessageNotFound:
                            errorMessage = MailScriptResource.AttachmentsMessageNotFoundError;
                            break;

                        case AttachmentsException.Types.TotalSizeExceeded:
                            errorMessage = MailScriptResource.AttachmentsTotalLimitError;
                            break;

                        case AttachmentsException.Types.DocumentNotFound:
                            errorMessage = MailScriptResource.AttachmentsDocumentNotFoundError;
                            break;

                        case AttachmentsException.Types.DocumentAccessDenied:
                            errorMessage = MailScriptResource.AttachmentsDocumentAccessDeniedError;
                            break;

                        default:
                            errorMessage = MailScriptResource.AttachmentsUnknownError;
                            break;
                        }
                        throw new Exception(errorMessage);
                    }
                    catch (TenantQuotaException)
                    {
                        throw;
                    }
                    catch (Exception)
                    {
                        throw new Exception(MailScriptResource.AttachmentsUnknownError);
                    }
                }
                throw new Exception(MailScriptResource.AttachmentsBadInputParamsError);
            }
            catch (Exception ex)
            {
                return(new FileUploadResult
                {
                    Success = false,
                    FileName = fileName,
                    Data = attachment,
                    Message = ex.Message,
                });
            }
        }
Example #6
0
        public override FileUploadResult ProcessUpload(HttpContext context)
        {
            var            file_name  = string.Empty;
            MailAttachment attachment = null;

            try
            {
                if (!SecurityContext.AuthenticateMe(CookiesManager.GetCookies(CookiesType.AuthKey)))
                {
                    throw new UnauthorizedAccessException(MailResource.AttachemntsUnauthorizedError);
                }

                if (FileToUpload.HasFilesToUpload(context))
                {
                    try
                    {
                        var stream_id = context.Request["stream"];
                        var mail_id   = Convert.ToInt32(context.Request["messageId"]);

                        if (mail_id < 1)
                        {
                            throw new AttachmentsException(AttachmentsException.Types.MESSAGE_NOT_FOUND,
                                                           "Message not yet saved!");
                        }

                        if (String.IsNullOrEmpty(stream_id))
                        {
                            throw new AttachmentsException(AttachmentsException.Types.BAD_PARAMS, "Have no stream");
                        }

                        var posted_file = new FileToUpload(context);

                        file_name = context.Request["name"];

                        attachment = new MailAttachment
                        {
                            fileId   = -1,
                            size     = posted_file.ContentLength,
                            fileName = file_name,
                            streamId = stream_id,
                            tenant   = TenantId,
                            user     = Username
                        };

                        attachment = _mailBoxManager.AttachFile(TenantId, Username, mail_id,
                                                                file_name, posted_file.InputStream, stream_id);

                        return(new FileUploadResult
                        {
                            Success = true,
                            FileName = attachment.fileName,
                            FileURL = attachment.storedFileUrl,
                            Data = attachment
                        });
                    }
                    catch (AttachmentsException e)
                    {
                        string error_message;

                        switch (e.ErrorType)
                        {
                        case AttachmentsException.Types.BAD_PARAMS:
                            error_message = MailScriptResource.AttachmentsBadInputParamsError;
                            break;

                        case AttachmentsException.Types.EMPTY_FILE:
                            error_message = MailScriptResource.AttachmentsEmptyFileNotSupportedError;
                            break;

                        case AttachmentsException.Types.MESSAGE_NOT_FOUND:
                            error_message = MailScriptResource.AttachmentsMessageNotFoundError;
                            break;

                        case AttachmentsException.Types.TOTAL_SIZE_EXCEEDED:
                            error_message = MailScriptResource.AttachmentsTotalLimitError;
                            break;

                        case AttachmentsException.Types.DOCUMENT_NOT_FOUND:
                            error_message = MailScriptResource.AttachmentsDocumentNotFoundError;
                            break;

                        case AttachmentsException.Types.DOCUMENT_ACCESS_DENIED:
                            error_message = MailScriptResource.AttachmentsDocumentAccessDeniedError;
                            break;

                        default:
                            error_message = MailScriptResource.AttachmentsUnknownError;
                            break;
                        }
                        throw new Exception(error_message);
                    }
                    catch (ASC.Core.Tenants.TenantQuotaException)
                    {
                        throw;
                    }
                    catch (Exception)
                    {
                        throw new Exception(MailScriptResource.AttachmentsUnknownError);
                    }
                }
                throw new Exception(MailScriptResource.AttachmentsBadInputParamsError);
            }
            catch (Exception ex)
            {
                return(new FileUploadResult
                {
                    Success = false,
                    FileName = file_name,
                    Data = attachment,
                    Message = ex.Message,
                });
            }
        }