/// <summary> /// Convert EWS attachment to System.Net.Mail.Attachment /// </summary> /// <param name="attachment">EWS attachment</param> /// <returns>Attachment</returns> public static System.Net.Mail.Attachment ToAttachment(this Microsoft.Exchange.WebServices.Data.Attachment attachment) { MemoryStream contentStream = new MemoryStream(); // Write the attachmentcontent to a MemoryStream if (attachment is ItemAttachment) { var itemAttachment = attachment as ItemAttachment; itemAttachment.Load(ItemSchema.MimeContent); contentStream = new MemoryStream(itemAttachment.Item.MimeContent.Content); } if (attachment is FileAttachment) { var fileAttachment = attachment as FileAttachment; fileAttachment.Load(contentStream); } var result = new System.Net.Mail.Attachment(contentStream, attachment.Name) { ContentId = attachment.ContentId, ContentType = new System.Net.Mime.ContentType(attachment.ContentType), }; result.ContentDisposition.CreationDate = attachment.LastModifiedTime; result.ContentDisposition.DispositionType = attachment.ContentType; result.ContentDisposition.FileName = attachment.Name; result.ContentDisposition.Inline = attachment.IsInline; result.ContentDisposition.ModificationDate = attachment.LastModifiedTime; result.ContentDisposition.Size = attachment.Size; return(result); }
public static void downloadWithUniqueName(this Microsoft.Exchange.WebServices.Data.Attachment att, ref string path, int mtaNum) { try { Directory.CreateDirectory(path); path += ("\\" + EvaultWin.Common.evWinHash.MD5Hasing.GetMD5HashFromText(mtaNum.ToString() + EvaultWin.Common.evWinRandom.winRandomNumber.GetRandomName(DateTime.Now.ToString())) + Path.GetExtension(att.Name)); if (att is FileAttachment) { FileAttachment fa = att as FileAttachment; fa.Load(path); } else { ItemAttachment itemAtt = att as ItemAttachment; itemAtt.Load(new PropertySet(EmailMessageSchema.MimeContent)); MessageBox.Show("is item Att with path : " + path); //path += ".eml"; byte[] mc = itemAtt.Item.MimeContent.Content; using (FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write)) { fs.Write(mc, 0, mc.Length); } } } catch (Exception err) { path = string.Empty; WinEventLog.WriteEventLog("MTA failed to download attachment for scanning, error = " + err.Message, EventLogEntryType.FailureAudit); EvaultWin.DLP.evWinExceptionHandlingCMC.ExceptionHandling.DetailEventLog(true, false, EvaultWin.DLP.evWinExceptionHandlingCMC.EventExceptionType.UnexpectedResult, EvaultWin.DLP.evWinExceptionHandlingCMC.EventSeverityLevel.Error, err.Message, err.Source.ToString() + " : " + err.TargetSite.ToString(), err.ToString().Replace("\r", "").ToString()); //MessageBox.Show("Failed to download attachment, err : " + err.Message); } }