Example #1
0
        public static InboundEmail IncludeAttachmentsOrDefault(this InboundEmail email, AttachmentCollection incomingAttachments)
        {
            if (incomingAttachments.Count <= 0)
            {
                return(email);
            }

            var filePath = ConfigurationManager.AppSettings["InboundAttachmentsPath"];

            var attachments = incomingAttachments.GetEnumerator();

            while (attachments.MoveNext())
            {
                var attachment = attachments.Current as MimePart;

                if (attachment != null && attachment.HasValidExtension()) // Skip invalid attachments
                {
                    var fileExtension = attachment.Filename.Split('.').LastOrDefault() ?? "INVALID";

                    if (filePath.Last() != '\\')
                    {
                        filePath = filePath + @"\";
                    }

                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }

                    var completeUniqueFilePath = GenerateUniqueFilePath(filePath, fileExtension);

                    attachment.StoreToFile(completeUniqueFilePath);

                    email.InboundAttachments.Add(new InboundAttachment
                    {
                        FilePath         = completeUniqueFilePath,
                        OriginalFileName = attachment.Filename,
                        FileType         = fileExtension.ToUpper()
                    });
                }
            }

            return(email);
        }