/// <summary> /// This function will read all the properties of an <see cref="Storage.Message"/> file and maps /// all the properties that are filled to the extended file attributes. /// </summary> /// <param name="inputFile">The msg file</param> public void SetExtendedFileAttributesWithMsgProperties(string inputFile) { MemoryStream memoryStream = null; try { // We need to read the msg file into memory because we otherwise can't set the extended filesystem // properties because the files is locked memoryStream = new MemoryStream(); using (var fileStream = File.OpenRead(inputFile)) fileStream.CopyTo(memoryStream); memoryStream.Position = 0; using (var shellFile = ShellFile.FromFilePath(inputFile)) { using (var propertyWriter = shellFile.Properties.GetPropertyWriter()) { using (var message = new Storage.Message(memoryStream)) { switch (message.Type) { case Storage.Message.MessageType.Email: MapEmailPropertiesToExtendedFileAttributes(message, propertyWriter); break; case Storage.Message.MessageType.AppointmentRequest: case Storage.Message.MessageType.Appointment: case Storage.Message.MessageType.AppointmentResponse: MapAppointmentPropertiesToExtendedFileAttributes(message, propertyWriter); break; case Storage.Message.MessageType.Task: case Storage.Message.MessageType.TaskRequestAccept: MapTaskPropertiesToExtendedFileAttributes(message, propertyWriter); break; case Storage.Message.MessageType.Contact: MapContactPropertiesToExtendedFileAttributes(message, propertyWriter); break; case Storage.Message.MessageType.Unknown: throw new NotSupportedException("Unsupported message type"); } } } } } finally { if (memoryStream != null) memoryStream.Dispose(); } }
public ToxyEmail Parse() { ToxyEmail result = new ToxyEmail(); using (var stream = File.OpenRead(this.Context.Path)) using (var reader = new Storage.Message(stream)) { if (reader.Sender != null) { result.From = string.IsNullOrEmpty(reader.Sender.DisplayName) ? reader.Sender.Email : string.Format("{0}<{1}>", reader.Sender.DisplayName, reader.Sender.Email); } if (reader.Recipients.Count > 0) { foreach (var recipient in reader.Recipients) { string sRecipient = null; if (string.IsNullOrEmpty(recipient.DisplayName)) { sRecipient = recipient.Email; } else { sRecipient = string.Format("{0}<{1}>", recipient.DisplayName, recipient.Email); } if (recipient.Type == MsgReader.Outlook.Storage.Recipient.RecipientType.To) { result.To.Add(sRecipient); } else if (recipient.Type == MsgReader.Outlook.Storage.Recipient.RecipientType.Cc) { result.Cc.Add(sRecipient); } else if (recipient.Type == MsgReader.Outlook.Storage.Recipient.RecipientType.Bcc) { result.Bcc.Add(sRecipient); } } } result.Subject = reader.Subject; result.TextBody = reader.BodyText; result.HtmlBody = reader.BodyHtml; } return result; }
public string[] ExtractToFolder(string inputFile, string outputFolder, bool hyperlinks = false) { outputFolder = FileManager.CheckForBackSlash(outputFolder); _errorMessage = string.Empty; var extension = CheckFileNameAndOutputFolder(inputFile, outputFolder); switch (extension) { case ".EML": using (var stream = File.Open(inputFile, FileMode.Open, FileAccess.Read)) { var message = Mime.Message.Load(stream); return WriteEmlEmail(message, outputFolder, hyperlinks).ToArray(); } case ".MSG": using (var stream = File.Open(inputFile, FileMode.Open, FileAccess.Read)) using (var message = new Storage.Message(stream)) { switch (message.Type) { case Storage.Message.MessageType.Email: case Storage.Message.MessageType.EmailSms: case Storage.Message.MessageType.EmailNonDeliveryReport: case Storage.Message.MessageType.EmailDeliveryReport: case Storage.Message.MessageType.EmailDelayedDeliveryReport: case Storage.Message.MessageType.EmailReadReceipt: case Storage.Message.MessageType.EmailNonReadReceipt: case Storage.Message.MessageType.EmailEncryptedAndMeabySigned: case Storage.Message.MessageType.EmailEncryptedAndMeabySignedNonDelivery: case Storage.Message.MessageType.EmailEncryptedAndMeabySignedDelivery: case Storage.Message.MessageType.EmailClearSignedReadReceipt: case Storage.Message.MessageType.EmailClearSignedNonDelivery: case Storage.Message.MessageType.EmailClearSignedDelivery: case Storage.Message.MessageType.EmailBmaStub: return WriteMsgEmail(message, outputFolder, hyperlinks).ToArray(); case Storage.Message.MessageType.EmailClearSigned: throw new MRFileTypeNotSupported("A clear signed message is not supported"); case Storage.Message.MessageType.Appointment: case Storage.Message.MessageType.AppointmentNotification: case Storage.Message.MessageType.AppointmentSchedule: case Storage.Message.MessageType.AppointmentRequest: case Storage.Message.MessageType.AppointmentRequestNonDelivery: case Storage.Message.MessageType.AppointmentResponse: case Storage.Message.MessageType.AppointmentResponsePositive: case Storage.Message.MessageType.AppointmentResponsePositiveNonDelivery: case Storage.Message.MessageType.AppointmentResponseNegative: case Storage.Message.MessageType.AppointmentResponseNegativeNonDelivery: case Storage.Message.MessageType.AppointmentResponseTentative: case Storage.Message.MessageType.AppointmentResponseTentativeNonDelivery: return WriteMsgAppointment(message, outputFolder, hyperlinks).ToArray(); case Storage.Message.MessageType.Contact: return WriteMsgContact(message, outputFolder, hyperlinks).ToArray(); case Storage.Message.MessageType.Task: case Storage.Message.MessageType.TaskRequestAccept: case Storage.Message.MessageType.TaskRequestDecline: case Storage.Message.MessageType.TaskRequestUpdate: return WriteMsgTask(message, outputFolder, hyperlinks).ToArray(); case Storage.Message.MessageType.StickyNote: return WriteMsgStickyNote(message, outputFolder).ToArray(); case Storage.Message.MessageType.Unknown: throw new MRFileTypeNotSupported("Unsupported message type"); } } break; } return new string[0]; }
static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Expected 2 command line arguments!"); Console.WriteLine("For example: EmailExtractor.exe \"c:\\<folder to read>\" \"c:\\filetowriteto.txt\""); return; } var folderIn = args[0]; if (!Directory.Exists(folderIn)) { Console.WriteLine("The directory '" + folderIn + "' does not exist"); return;; } var toFile = args[1]; // Get all the .msg files from the folder var files = new DirectoryInfo(folderIn).GetFiles("*.msg"); Console.WriteLine("Found '" + files.Count() + "' files to process"); // Loop through all the files foreach (var file in files) { Console.WriteLine("Checking file '" + file.FullName + "'"); // Load the msg file using (var message = new Storage.Message(file.FullName)) { Console.WriteLine("Found '" + message.Attachments.Count + "' attachments"); // Loop through all the attachments foreach (var attachment in message.Attachments) { // Try to cast the attachment to a Message file var msg = attachment as Storage.Message; // If the file not is null then we have an msg file if (msg != null) { using (msg) { Console.WriteLine("Found msg file '" + msg.Subject + "'"); if (!string.IsNullOrWhiteSpace(msg.MailingListSubscribe)) Console.WriteLine("Mailing list subscribe page: '" + msg.MailingListSubscribe + "'"); foreach (var recipient in msg.Recipients) { if (!string.IsNullOrWhiteSpace(recipient.Email)) { Console.WriteLine("Recipient E-mail: '" + recipient.Email + "'"); File.AppendAllText(toFile, recipient.Email + Environment.NewLine); } else if (!string.IsNullOrWhiteSpace(recipient.DisplayName)) { Console.WriteLine("Recipient display name: '" + recipient.DisplayName + "'"); File.AppendAllText(toFile, recipient.DisplayName + Environment.NewLine); } } } } } } } }
public override string Parse() { StringBuilder result = new StringBuilder(); using (var stream = File.OpenRead(this.Context.Path)) using (var reader = new Storage.Message(stream)) { if (reader.Sender != null) { result.AppendFormat("[From] {0}{1}", string.IsNullOrEmpty(reader.Sender.DisplayName) ? reader.Sender.Email : string.Format("{0}<{1}>", reader.Sender.DisplayName, reader.Sender.Email), Environment.NewLine); } if (reader.Recipients.Count > 0) { StringBuilder recipientTo = new StringBuilder(); StringBuilder recipientCc = new StringBuilder(); StringBuilder recipientBcc = new StringBuilder(); foreach (var recipient in reader.Recipients) { string sRecipient = null; if (string.IsNullOrEmpty(recipient.DisplayName)) { sRecipient = recipient.Email+";"; } else { sRecipient = string.Format("{0}<{1}>;", recipient.DisplayName, recipient.Email); } if (recipient.Type == MsgReader.Outlook.Storage.Recipient.RecipientType.To) { recipientTo.Append(sRecipient); } else if (recipient.Type == MsgReader.Outlook.Storage.Recipient.RecipientType.Cc) { recipientCc.Append(sRecipient); } else if (recipient.Type == MsgReader.Outlook.Storage.Recipient.RecipientType.Bcc) { recipientBcc.Append(sRecipient); } } if (recipientTo.Length > 0) { result.Append("[To] "); result.AppendLine(recipientTo.ToString()); } if (recipientCc.Length > 0) { result.Append("[Cc] "); result.AppendLine(recipientCc.ToString()); } if (recipientBcc.Length>0) { result.Append("[Bcc] "); result.AppendLine(recipientBcc.ToString()); } } if (!string.IsNullOrEmpty(reader.Subject)) result.AppendFormat("[Subject] {0}{1}", reader.Subject, Environment.NewLine); result.AppendLine(); result.AppendLine(reader.BodyText); } return result.ToString(); }
protected async override Task<ProcessResult> OnPolling(Shared.Jobs.PollerJobParameters parameters, string workingFolder) { string localFile = await DownloadBlob( parameters.TenantId, parameters.JobId, parameters.FileName, workingFolder); String[] permittedExtension = null; if (parameters.All.ContainsKey("extensions")) { var extensionsPermitted = parameters.All["extensions"]; if (extensionsPermitted != "*") { permittedExtension = extensionsPermitted.Split('|'); } } var extension = Path.GetExtension(localFile); var unzippingDirectory = new DirectoryInfo( Path.Combine(workingFolder, Guid.NewGuid().ToString())).FullName; if (!Directory.Exists(unzippingDirectory)) Directory.CreateDirectory(unzippingDirectory); if (extension == ".zip") { //we can handle unzipping everything. ZipFile.ExtractToDirectory(localFile, unzippingDirectory); IEnumerable<String> files = Directory.EnumerateFiles(unzippingDirectory, "*.*", SearchOption.AllDirectories); Int32 uploadCount = await UploadAttachmentListToDocumentStore(parameters, permittedExtension, unzippingDirectory, files); Logger.DebugFormat("Uploaded {0} attachments", uploadCount); } else if (extension == ".eml") { using (var stream = File.Open(localFile, FileMode.Open, FileAccess.Read)) { var message = MsgReader.Mime.Message.Load(stream); var bodyPart = message.HtmlBody ?? message.TextBody; String body = ""; if (bodyPart != null) body = bodyPart.GetBodyAsText(); foreach (MsgReader.Mime.MessagePart attachment in message.Attachments.OfType<MsgReader.Mime.MessagePart>()) { if (!String.IsNullOrEmpty(attachment.ContentId) && body.Contains(attachment.ContentId)) { if (Logger.IsDebugEnabled) { Logger.DebugFormat("Attachment cid {0} name {1} discharded because it is inline", attachment.ContentId, attachment.FileName); continue; } } String fileName = Path.Combine(unzippingDirectory, attachment.FileName); File.WriteAllBytes(fileName, attachment.Body); await AddAttachmentToHandle( parameters.TenantId, parameters.JobId, fileName, "attachment_email", attachment.FileName, new Dictionary<string, object>() { } ); } } } else if (extension == ".msg") { using (var stream = File.Open(localFile, FileMode.Open, FileAccess.Read)) using (var message = new Storage.Message(stream)) { foreach (Storage.Attachment attachment in message.Attachments.OfType<Storage.Attachment>()) { if (attachment.IsInline) continue; //no need to uncompress inline attqach String fileName = Path.Combine(unzippingDirectory, attachment.FileName); File.WriteAllBytes(fileName, attachment.Data); await AddAttachmentToHandle( parameters.TenantId, parameters.JobId, fileName, "attachment_email", attachment.FileName, new Dictionary<string, object>() { } ); } } } else if (extension == ".7z" || extension == ".7zip" || extension == ".rar") { //we can handle unzipping everything. var extracted = _sevenZipExtractorFunctions.ExtractTo(localFile, unzippingDirectory); Int32 uploadCount = await UploadAttachmentListToDocumentStore(parameters, permittedExtension, unzippingDirectory, extracted); Logger.DebugFormat("Uploaded {0} attachments", uploadCount); } return ProcessResult.Ok; }