Example #1
0
        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.AttachFileToDraft(TenantId, Username, id_message, calendar.Method.ToLowerInvariant() + ".ics", ms, "text/calendar");
                    return(attachment);
                }
            }
        }
        private void SaveIcsAttachment(MailDraft draft, MimeMessage mimeMessage)
        {
            if (string.IsNullOrEmpty(draft.CalendarIcs))
            {
                return;
            }

            try
            {
                var icsAttachment =
                    mimeMessage.Attachments.FirstOrDefault(
                        a => a.ContentType.IsMimeType("application", "ics"));

                if (icsAttachment == null)
                {
                    return;
                }

                using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(draft.CalendarIcs)))
                {
                    manager.AttachFileToDraft(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));
            }
        }
        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, new MailMessage.Options());
                        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 = MailBoxManager.AttachFileToDraft(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 #4
0
        public MailAttachment AddAttachment(int id_message, string name, Stream file, string content_type)
        {
            var attachment = MailBoxManager.AttachFileToDraft(TenantId, Username, id_message, name, file, content_type);

            return(attachment);
        }