public SMIMETypeField(ContentTypeField field) { Type = field.Type; SubType = field.SubType; Body = field.Body; Name = field.Name; Parameters = field.Parameters; }
/// <summary> /// Creates a new instance of the given field type. /// </summary> /// <param name="fieldType">The field type</param> /// <returns>The new instance</returns> private object CreateField(ContentTypeField fieldType) { var type = App.Fields.GetByType(fieldType.Type); if (type != null) { return(Activator.CreateInstance(type.Type)); } return(null); }
// Parses the content of the specified mime type. // Recognized mime types are "text/html" and "text/plain". private static string ParseBodyFromMime(Entity mimeEntity, string mimeType) { string parsedText = string.Empty; if (mimeEntity.IsMultipart) { Multipart multiPart = (Multipart)mimeEntity.Body; foreach (Entity part in multiPart.BodyParts) { ContentTypeField contentType = part.Header.GetField(MimeField.ContentType) as ContentTypeField; MimeField contentDispositionField = part.Header.GetField("Content-Disposition"); if (contentType == null) { continue; } if (contentDispositionField != null && contentDispositionField.Body.StartsWith("attachment")) { // An attached text file could also be ITextBody. We don't want this. We only want message body. continue; } if (part.Body is ITextBody && contentType.MimeType.Contains(mimeType)) { parsedText = ParseTextBody(part); } else if (part.IsMultipart) { parsedText = ParseBodyFromMime((Entity)part, mimeType); } else if (part.Body is MimeMessage) { parsedText = ParseBodyFromMime((MimeMessage)part.Body, mimeType); } } } else { ContentTypeField contentType = mimeEntity.Header.GetField(MimeField.ContentType) as ContentTypeField; if (contentType != null) { if (mimeEntity.Body is ITextBody && contentType.MimeType.Contains(mimeType)) { parsedText = ParseTextBody(mimeEntity); } } } return(parsedText); }
private Tuple <int?, ContentTypeField> GetFieldType(PropertyInfo prop) { var attr = prop.GetCustomAttribute <FieldAttribute>(); if (attr != null) { var appFieldType = App.Fields.GetByType(prop.PropertyType); // Missing field type, check if we can register it on the fly if (appFieldType == null) { RegisterField(prop.PropertyType); appFieldType = App.Fields.GetByType(prop.PropertyType); } if (appFieldType != null) { var fieldType = new ContentTypeField { Id = prop.Name, Title = attr.Title, Type = appFieldType.TypeName, Options = attr.Options, Placeholder = attr.Placeholder }; int?sortOrder = attr.SortOrder != Int32.MaxValue ? attr.SortOrder : (int?)null; // Get optional description var descAttr = prop.GetCustomAttribute <FieldDescriptionAttribute>(); if (descAttr != null) { fieldType.Description = descAttr.Text; } // Get optional settings fieldType.Settings = Utils.GetFieldSettings(prop); return(new Tuple <int?, ContentTypeField>(sortOrder, fieldType)); } } return(null); }
// Parses the content of the specified mime type. // Recognized mime types are "text/html" and "text/plain". private static string ParseFromMime(Entity mimeEntity, string mimeType) { string parsedText = string.Empty; if (mimeEntity.IsMultipart) { Multipart multiPart = (Multipart)mimeEntity.Body; foreach (Entity part in multiPart.BodyParts) { ContentTypeField contentType = part.Header.GetField(MimeField.ContentType) as ContentTypeField; if (contentType == null) { continue; } if (part.Body is ITextBody && contentType.MimeType.Contains(mimeType)) { parsedText = ParseTextBody(part); } else if (part.IsMultipart) { parsedText = ParseFromMime((Entity)part, mimeType); } else if (part.Body is MimeMessage) { parsedText = ParseFromMime((MimeMessage)part.Body, mimeType); } } } else { ContentTypeField contentType = mimeEntity.Header.GetField(MimeField.ContentType) as ContentTypeField; if (contentType != null) { if (mimeEntity.Body is ITextBody && contentType.MimeType.Contains(mimeType)) { parsedText = ParseTextBody(mimeEntity); } } } return(parsedText); }
private ContentTypeField GetFieldType(PropertyInfo prop) { var attr = prop.GetCustomAttribute <FieldAttribute>(); if (attr != null) { var appFieldType = App.Fields.GetByType(prop.PropertyType); // Missing field type, check if we can register it on the fly if (appFieldType == null) { RegisterField(prop.PropertyType); appFieldType = App.Fields.GetByType(prop.PropertyType); } if (appFieldType != null) { var fieldType = new ContentTypeField { Id = prop.Name, Title = attr.Title, Type = appFieldType.TypeName, Options = attr.Options, Placeholder = attr.Placeholder }; // Get optional description var descAttr = prop.GetCustomAttribute <FieldDescriptionAttribute>(); if (descAttr != null) { fieldType.Description = descAttr.Text; } return(fieldType); } } return(null); }
// Extracts binary contents from mime and saves them to given location. // Inserts <content-ID, saved file name> pairs to the dictionary. private static void SaveAttachments(Entity mimeEntity, string savePath, Dictionary <string, string> savedFiles) { if (mimeEntity.IsMultipart) { foreach (Entity part in ((Multipart)mimeEntity.Body).BodyParts) { ContentTypeField contentType = part.Header.GetField(MimeField.ContentType) as ContentTypeField; if (contentType == null) { continue; } if (part.Body is MimeMessage) { SaveAttachments((MimeMessage)part.Body, savePath, savedFiles); } else if (part.IsMultipart) { SaveAttachments((Entity)part, savePath, savedFiles); } else if (!(part.Body is ITextBody)) { string fileName; if (contentType.Parameters.Contains("name")) { string name = contentType.Parameters["name"].ToString(); if (IsValidFileName(name)) { fileName = name; } else { fileName = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".attachment"; } } else { fileName = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".attachment"; } string filePath = Path.Combine(savePath, fileName); FileStream outFileStream = new FileStream(filePath, FileMode.Create); BinaryReader rdr = ((IBinaryBody)part.Body).Reader; byte[] buf = new byte[1024]; int bytesRead = 0; while ((bytesRead = rdr.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, bytesRead); } outFileStream.Flush(); outFileStream.Close(); MimeField contentIdField = part.Header.GetField("Content-ID"); string key = (contentIdField != null) ? contentIdField.Body.Trim('"', '<', '>', ' ') : fileName; if (!savedFiles.ContainsKey(key)) { savedFiles.Add(key, filePath); } } } } }
private ContentTypeField GetFieldType(PropertyInfo prop) { var attr = prop.GetCustomAttribute <FieldAttribute>(); if (attr != null) { var appFieldType = App.Fields.GetByType(prop.PropertyType); // Missing field type, check if we can register it on the fly if (appFieldType == null) { RegisterField(prop.PropertyType); appFieldType = App.Fields.GetByType(prop.PropertyType); } if (appFieldType != null) { var fieldType = new ContentTypeField { Id = prop.Name, Title = attr.Title, Type = appFieldType.TypeName, Options = attr.Options, Placeholder = attr.Placeholder }; // Get optional description var descAttr = prop.GetCustomAttribute <FieldDescriptionAttribute>(); if (descAttr != null) { fieldType.Description = descAttr.Text; } // Get optional settings var settingsAttr = prop.GetCustomAttribute <FieldSettingsAttribute>(); if (settingsAttr != null) { foreach (var setting in settingsAttr.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) { if (fieldType.Settings.TryGetValue(setting.Name, out var existing)) { if (!(existing is IList)) { existing = new ArrayList() { existing }; fieldType.Settings[setting.Name] = existing; } ((IList)existing).Add(setting.GetValue(settingsAttr)); } else { fieldType.Settings[setting.Name] = setting.GetValue(settingsAttr); } } } return(fieldType); } } return(null); }
// Extracts binary contents from mime and saves them to given location. // Inserts <content-ID, saved file path> pairs to the dictionary. private static void SaveAttachments(Entity mimeEntity, string savePath, Dictionary <string, string> savedFiles) { if (mimeEntity.IsMultipart) { foreach (Entity part in ((Multipart)mimeEntity.Body).BodyParts) { ContentTypeField contentType = part.Header.GetField(MimeField.ContentType) as ContentTypeField; if (contentType == null) { continue; } if (part.Body is MimeMessage) { SaveAttachments((MimeMessage)part.Body, savePath, savedFiles); } else if (part.IsMultipart) { SaveAttachments((Entity)part, savePath, savedFiles); } else { MimeField contentDispositionField = part.Header.GetField("Content-Disposition"); if (contentDispositionField == null) { continue; } if (!contentDispositionField.Body.StartsWith("attachment")) { continue; } string fileName; Match fileNameMatch = Regex.Match(contentDispositionField.Body, "filename=\"?([^<>\"]+)"); if (fileNameMatch.Success) { string name = fileNameMatch.Groups[1].Value.Trim(' ', '"', '<', '>'); name = Decoder.DecodeSingleLine(name); if (IsValidFileName(name)) { fileName = name; } else { fileName = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".attachment"; } } else if (contentType.Parameters.Contains("name")) { string name = contentType.Parameters["name"].ToString(); if (IsValidFileName(name)) { fileName = name; } else { fileName = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".attachment"; } } else { fileName = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".attachment"; } string filePath = Path.Combine(savePath, fileName); if (part.Body is ITextBody) { try { string parsedText = ParseTextBody(part); File.WriteAllText(filePath, parsedText); savedFiles.Add(fileName, filePath); } catch (Exception e) { Trace.WriteLine("SaveAttachments: " + e.Message); } } else { try { FileStream outFileStream = new FileStream(filePath, FileMode.Create); BinaryReader rdr = ((IBinaryBody)part.Body).Reader; byte[] buf = new byte[1024]; int bytesRead = 0; while ((bytesRead = rdr.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, bytesRead); } outFileStream.Flush(); outFileStream.Close(); savedFiles.Add(fileName, filePath); } catch (Exception e) { Trace.WriteLine("SaveAttachments: " + e.Message); } } } } } }