public static AttachmentStream GetAttachmentStream(MailAttachment attachment)
 {
     if (attachment != null)
     {
         var storage = MailDataStore.GetDataStore(attachment.tenant);
         var attachment_path = MailStoragePathCombiner.GerStoredFilePath(attachment);
         var result = new AttachmentStream
         {
             FileStream = storage.GetReadStream("", attachment_path),
             FileName = attachment.fileName
         };
         return result;
     }
     throw new InvalidOperationException("Attachment not found");
 }
 public MailAttachment ToMailAttachment(string link)
 {
     var fileName = Path.GetFileName(link);
     if (!_loadedSmiles.ContainsKey(link))
     {
         var attach = new MailAttachment
                         {
                             fileName = fileName,
                             storedName = fileName,
                             contentId = link.GetMd5(),
                             data = StorageManager.LoadLinkData(link)
                         };
         _loadedSmiles[link] = attach;
     }
     return _loadedSmiles[link];
 }
 public static string GerStoredFilePath(MailAttachment attachment)
 {
     return GetFileKey(attachment.user, attachment.streamId, attachment.fileNumber, attachment.storedName);
 }
 public static string GetPreSignedUrl(MailAttachment attachment, IDataStore data_store = null)
 {
     return GetPreSignedUri(attachment.fileId, attachment.tenant, attachment.user, attachment.streamId, attachment.fileNumber, attachment.storedName, data_store);
 }
Esempio n. 5
0
        public static MailMessage CreateCorruptedMesage(MimeMessage message, int folder = 1,
                                                        bool unread = false, string chainId = "", string streamId = "")
        {
            var mailMessage = new MailMessage
            {
                HasParseError = true
            };

            SkipErrors(() => mailMessage.Date = IsDateCorrect(message.Date.UtcDateTime)
                ? message.Date.UtcDateTime
                : DateTime.UtcNow);

            SkipErrors(() => mailMessage.MimeMessageId = (string.IsNullOrEmpty(message.MessageId) ? MailUtil.CreateMessageId() : message.MessageId)
                                                         .Trim('<', '>'));

            SkipErrors(() => mailMessage.ChainId = string.IsNullOrEmpty(chainId) ? mailMessage.MimeMessageId : chainId);

            SkipErrors(() => mailMessage.MimeReplyToId = mailMessage.ChainId.Equals(mailMessage.MimeMessageId) ? null : message.InReplyTo.Trim('<', '>'));

            SkipErrors(() => mailMessage.ReplyTo = message.ReplyTo.ToString());

            SkipErrors(() => mailMessage.From = message.From.ToString());

            SkipErrors(() =>
                       mailMessage.FromEmail =
                           message.From != null && message.From.Mailboxes != null && message.From.Mailboxes.Any()
                        ? message.From.Mailboxes.First().Address
                        : "");

            SkipErrors(() => mailMessage.ToList = message.To.Mailboxes.Select(s => ExecuteSafe(() => new MailAddress(s.Address, s.Name))).ToList());

            SkipErrors(() => mailMessage.To = string.Join(", ", message.To.Mailboxes.Select(s => s.ToString())));

            SkipErrors(() => mailMessage.CcList = message.Cc.Mailboxes.Select(s => ExecuteSafe(() => new MailAddress(s.Address, s.Name))).ToList());

            SkipErrors(() => mailMessage.Cc = string.Join(", ", message.Cc.Mailboxes.Select(s => s.ToString())));

            SkipErrors(() => mailMessage.Bcc = string.Join(", ", message.Bcc.Mailboxes.Select(s => s.ToString())));

            SkipErrors(() => mailMessage.Subject = message.Subject ?? string.Empty);

            SkipErrors(() => mailMessage.Important = message.Importance == MessageImportance.High || message.Priority == MessagePriority.Urgent);

            mailMessage.HtmlBodyStream = new MemoryStream();

            using (var sw = new StreamWriter(mailMessage.HtmlBodyStream, Encoding.UTF8, 1024, true))
            {
                sw.Write("<body><pre>&nbsp;</pre></body>");
                sw.Flush();
            }

            mailMessage.Size = mailMessage.HtmlBodyStream.Length;

            mailMessage.HeaderFieldNames = new NameValueCollection();

            message.Headers
            .ToList()
            .ForEach(h => SkipErrors(() => mailMessage.HeaderFieldNames.Add(h.Field, h.Value)));

            mailMessage.Folder       = folder;
            mailMessage.IsNew        = unread;
            mailMessage.StreamId     = string.IsNullOrEmpty(streamId) ? MailUtil.CreateStreamId() : streamId;
            mailMessage.TextBodyOnly = true;
            mailMessage.Introduction = "";
            mailMessage.Attachments  = new List <MailAttachment>();

            SkipErrors(() =>
            {
                var mailAttach = new MailAttachment
                {
                    contentId       = null,
                    fileName        = "message.eml",
                    contentType     = "message/rfc822",
                    contentLocation = null,
                    dataStream      = new MemoryStream()
                };

                message.WriteTo(mailAttach.dataStream);

                mailAttach.size = mailAttach.dataStream.Length;

                mailMessage.Attachments.Add(mailAttach);
            });

            return(mailMessage);
        }