Beispiel #1
0
        private static MimeEntity GetPart(Message message, string id)
        {
            MessageEntitySummary part = message.Parts.Flatten(p => p.ChildParts).FirstOrDefault(p => p.Id == id);

            if (part == null)
            {
                throw new FileNotFoundException($"Part with id '{id}' in message {message.Id} is not found");
            }

            return(part.MimeEntity);
        }
Beispiel #2
0
        private MessageEntitySummary HandleMimeEntity(MimeEntity entity)
        {
            int index = 0;

            return(MimeEntityVisitor.VisitWithResults <MessageEntitySummary>(entity, (e, p) =>
            {
                MessageEntitySummary result = new MessageEntitySummary()
                {
                    MessageId = Id,
                    Id = index.ToString(),
                    ContentId = e.ContentId,
                    Name = e.ContentId + " - " + e.ContentType.MimeType,
                    Headers = e.Headers.Select(h => new Header {
                        Name = h.Field, Value = h.Value
                    }).ToList(),
                    ChildParts = new List <MessageEntitySummary>(),
                    Attachments = new List <AttachmentSummary>(),
                    Size = e.ToString().Length,
                    MimeEntity = e
                };

                if (p != null)
                {
                    p.ChildParts.Add(result);

                    if (e.IsAttachment)
                    {
                        p.Attachments.Add(new AttachmentSummary()
                        {
                            Id = result.Id,
                            ContentId = result.ContentId,
                            FileName = string.IsNullOrEmpty(e.ContentType?.Name)
                                                           ? e.ContentDisposition?.FileName
                                                           : e.ContentType.Name,
                            Url = $"/api/messages/{Id}/part/{result.Id}/content"
                        });
                    }
                }

                index++;
                return result;
            }));
        }
Beispiel #3
0
        private MessageEntitySummary HandleMimeEntity(MimeEntity entity)
        {
            int index = 0;

            return(MimeEntityVisitor.Visit <MessageEntitySummary>(entity, null, (e, p) =>
            {
                string cid = e.ContentId ?? index.ToString();

                MessageEntitySummary result = new MessageEntitySummary()
                {
                    MessageId = Id,
                    ContentId = cid,
                    Name = e.ContentId + " - " + e.ContentType.MimeType,
                    Headers = e.Headers.Select(h => new Header {
                        Name = h.Field, Value = h.Value
                    }).ToList(),
                    ChildParts = new List <MessageEntitySummary>(),
                    Attachments = new List <AttachmentSummary>()
                };

                if (p != null)
                {
                    p.ChildParts.Add(result);

                    if (e.IsAttachment)
                    {
                        p.Attachments.Add(new AttachmentSummary()
                        {
                            ContentId = result.ContentId,
                            FileName = e.ContentDisposition?.FileName,
                            Url = $"/api/messages/{Id}/part/{result.ContentId}/content"
                        });
                    }
                }

                index++;
                return result;
            }));
        }
Beispiel #4
0
        private MessageEntitySummary HandleMimeEntity(MimeEntity entity)
        {
            MessageEntitySummary result = new MessageEntitySummary()
            {
                Name    = entity.ContentId + " - " + entity.ContentType.MimeType,
                Headers = entity.Headers.Select(h => new Header {
                    Name = h.Field, Value = h.Value
                }).ToList(),
                ChildParts = new List <MessageEntitySummary>(),
                Source     = entity.ToString()
            };

            using (MemoryStream memoryStream = new MemoryStream())
            {
                entity.WriteTo(memoryStream, true);
                result.Body = Encoding.UTF8.GetString(memoryStream.ToArray());
            }


            if (entity is Multipart multipart)
            {
                foreach (MimeEntity childEntity in multipart)
                {
                    result.ChildParts.Add(HandleMimeEntity(childEntity));
                }
            }
            else if (entity is MimeKit.MessagePart rfc822)
            {
                result.Html = rfc822.Message.HtmlBody;
                result.ChildParts.Add(HandleMimeEntity(rfc822.Message.Body));
            }
            else
            {
                var part = (MimePart)entity;
            }

            return(result);
        }
Beispiel #5
0
        private MessageEntitySummary HandleMimeEntity(MimeEntity entity)
        {
            int index = 0;

            return(MimeEntityVisitor.VisitWithResults <MessageEntitySummary>(entity, (e, p) =>
            {
                string fileName = PunyCodeReplacer.DecodePunycode(!string.IsNullOrEmpty(e.ContentDisposition?.FileName)
            ? e.ContentDisposition?.FileName
            : e.ContentType?.Name);

                MessageEntitySummary result = new MessageEntitySummary()
                {
                    MessageId = Id,
                    Id = index.ToString(),
                    ContentId = e.ContentId,
                    Name = (fileName ?? e.ContentId ?? index.ToString()) + " - " + e.ContentType.MimeType,
                    Headers = e.Headers.Select(h => new Header {
                        Name = h.Field, Value = PunyCodeReplacer.DecodePunycode(h.Value)
                    }).ToList(),
                    ChildParts = new List <MessageEntitySummary>(),
                    Attachments = new List <AttachmentSummary>(),
                    Warnings = new List <MessageWarning>(),
                    Size = e.ToString().Length,
                    IsAttachment = (e.ContentDisposition?.Disposition != "inline" && !string.IsNullOrEmpty(fileName)) || e.ContentDisposition?.Disposition == "attachment",
                    MimeEntity = e
                };

                if (p != null)
                {
                    p.ChildParts.Add(result);

                    if (result.IsAttachment)
                    {
                        if (e.ContentDisposition?.Disposition != "attachment")
                        {
                            result.Warnings.Add(new MessageWarning {
                                Details = $"Attachment '{fileName}' should have \"Content-Disposition: attachment\" header."
                            });
                        }

                        if (string.IsNullOrEmpty(fileName))
                        {
                            result.Warnings.Add(new MessageWarning {
                                Details = $"Attachment with content ID '{e.ContentId}' should have filename specified in either 'Content-Type' or 'Content-Disposition' header."
                            });
                        }

                        p.Attachments.Add(new AttachmentSummary()
                        {
                            Id = result.Id,
                            ContentId = result.ContentId,
                            FileName = fileName,
                            Url = $"api/messages/{Id}/part/{result.Id}/content"
                        });
                    }
                }

                index++;
                return result;
            }));
        }