private static MailMessage ParseItem(ex.GetItemResponse item) { var mailMessage = new MailMessage(); var exchangeSenderEmail = (ex.EmailAddress)item.Item[ex.EmailMessageSchema.From]; mailMessage.From = new MailAddress(exchangeSenderEmail.Address, exchangeSenderEmail.Name); foreach (var emailAddress in ((ex.EmailAddressCollection)item.Item[ex.EmailMessageSchema.ToRecipients])) { mailMessage.To.Add(new MailAddress(emailAddress.Address, emailAddress.Name)); } mailMessage.Subject = item.Item.Subject; mailMessage.Body = item.Item.Body.ToString(); var attachmentCollection = item.Item[ex.ItemSchema.Attachments]; foreach (var attachment in (ex.AttachmentCollection)attachmentCollection) { if (!(attachment is ex.FileAttachment fileAttachment)) { continue; } fileAttachment.Load(); var tempFile = Path.GetTempFileName(); File.WriteAllBytes(tempFile, fileAttachment.Content); var ms = new MemoryStream(File.ReadAllBytes(tempFile)); mailMessage.Attachments.Add(new Attachment(ms, attachment.Name ?? tempFile)); File.Delete(tempFile); } return(mailMessage); }
/// <summary> /// Dump an error response as XML /// </summary> /// <param name="response">Response to get data from</param> /// <param name="destinationFolderPath">Folder to save messages to</param> private static void DumpErrorResponseXML( GetItemResponse response, string destinationFolderPath) { DebugLog.WriteVerbose("Writing error to file."); XmlDocument xmlDoc = new XmlDocument(); XmlNode xmlMessage = xmlDoc.CreateNode(XmlNodeType.Element, "Message", string.Empty); xmlDoc.AppendChild(xmlMessage); XmlNode xmlProperties = xmlDoc.CreateNode(XmlNodeType.Element, "Properties", string.Empty); xmlMessage.AppendChild(xmlProperties); // Create a file path to save the error message to string fileName = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}\\_ERROR.txt", destinationFolderPath); // Get error message contents XmlNode xmlError = xmlDoc.CreateNode(XmlNodeType.Element, "Error", string.Empty); XmlNode xmlErrorCode = xmlDoc.CreateNode(XmlNodeType.Element, "ErrorCode", string.Empty); xmlErrorCode.InnerText = response.ErrorCode.ToString(); xmlError.AppendChild(xmlErrorCode); XmlNode xmlErrorMessage = xmlDoc.CreateNode(XmlNodeType.Element, "ErrorMessage", string.Empty); xmlErrorMessage.InnerText = response.ErrorMessage; xmlError.AppendChild(xmlErrorMessage); if (response.ErrorDetails != null && response.ErrorDetails.Count > 0) { XmlNode xmlErrorDetails = xmlDoc.CreateNode(XmlNodeType.Element, "ErrorDetails", string.Empty); StringBuilder detailsText = new StringBuilder(); foreach (KeyValuePair<string, string> detail in response.ErrorDetails) { XmlNode xmlErrorDetail = xmlDoc.CreateNode(XmlNodeType.Element, "ErrorDetail", string.Empty); XmlNode xmlKeyAttr = xmlDoc.CreateNode(XmlNodeType.Attribute, "Key", string.Empty); xmlKeyAttr.Value = detail.Key; xmlErrorDetail.AppendChild(xmlKeyAttr); xmlErrorDetail.InnerText = detail.Value; xmlErrorDetails.AppendChild(xmlErrorDetail); } xmlError.AppendChild(xmlErrorDetails); } if (response.ErrorProperties != null && response.ErrorProperties.Count > 0) { XmlNode xmlErrorProps = xmlDoc.CreateNode(XmlNodeType.Element, "ErrorProperties", string.Empty); StringBuilder propsText = new StringBuilder(); foreach (PropertyDefinitionBase baseProp in response.ErrorProperties) { PropertyInterpretation prop = new PropertyInterpretation(response.Item, baseProp); xmlErrorProps.AppendChild(prop.ToXML(xmlDoc)); } xmlError.AppendChild(xmlErrorProps); } xmlProperties.AppendChild(xmlError); // Write MIME content to file System.IO.File.WriteAllText( FileHelper.EnsureUniqueFileName(fileName), xmlDoc.OuterXml); DebugLog.WriteVerbose(String.Format("Wrote error to file, {0}.", fileName)); }