GroupDocs.Watermark allows you to get the information about the attachments in an email message. Below is the code snippet for extracting attachments from Email.
EmailLoadOptionsloadOptions=newEmailLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"using(Watermarkerwatermarker=newWatermarker("message.msg",loadOptions)){EmailContentcontent=watermarker.GetContent<EmailContent>();foreach(EmailAttachmentattachmentincontent.Attachments){Console.WriteLine("Name: {0}",attachment.Name);Console.WriteLine("File format: {0}",attachment.GetDocumentInfo().FileType);File.WriteAllBytes(Path.Combine("SampleFiles\Output",attachment.Name),attachment.Content);}}
Removing particular attachment from email message
Using GroupDocs.Watermark, you can remove any particular attachment from an email message. Below is the code for removing specific attachment:
EmailLoadOptionsloadOptions=newEmailLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"using(Watermarkerwatermarker=newWatermarker("message.msg",loadOptions)){EmailContentcontent=watermarker.GetContent<EmailContent>();for(inti=content.Attachments.Count-1;i>=0;i--){EmailAttachmentattachment=content.Attachments[i];// Remove all attached files with a particular name and formatif(attachment.Name.Contains("sample")&&attachment.GetDocumentInfo().FileType==FileType.DOCX){content.Attachments.RemoveAt(i);}}// Save changeswatermarker.Save("message.msg");}
Adding attachment to email message
You can also add attachments to the email messages using GroupDocs.Watermark. Following is the code sample for adding an attachment:
EmailLoadOptionsloadOptions=newEmailLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"using(Watermarkerwatermarker=newWatermarker("message.msg",loadOptions)){EmailContentcontent=watermarker.GetContent<EmailContent>();content.Attachments.Add(File.ReadAllBytes("sample.msg"),"sample.msg");// Save changeswatermarker.Save("message.msg");}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.