Email attachments

Extracting all attachments from email message

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.

AdvancedUsage.AddingWatermarks.AddWatermarksToEmailAttachments.EmailExtractAllAttachments

EmailLoadOptions loadOptions = new EmailLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"
using (Watermarker watermarker = new Watermarker("message.msg", loadOptions))
{
    EmailContent content = watermarker.GetContent<EmailContent>();
    foreach (EmailAttachment attachment in content.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:

AdvancedUsage.AddingWatermarks.AddWatermarksToEmailAttachments.EmailRemoveAttachment

EmailLoadOptions loadOptions = new EmailLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"
using (Watermarker watermarker = new Watermarker("message.msg", loadOptions))
{
    EmailContent content = watermarker.GetContent<EmailContent>();
    for (int i = content.Attachments.Count - 1; i >= 0; i--)
    {
        EmailAttachment attachment = content.Attachments[i];

        // Remove all attached files with a particular name and format
        if (attachment.Name.Contains("sample") && attachment.GetDocumentInfo().FileType == FileType.DOCX)
        {
            content.Attachments.RemoveAt(i);
        }
    }

    // Save changes
    watermarker.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:

AdvancedUsage.AddingWatermarks.AddWatermarksToEmailAttachments.EmailAddAttachment

EmailLoadOptions loadOptions = new EmailLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"
using (Watermarker watermarker = new Watermarker("message.msg", loadOptions))
{
    EmailContent content = watermarker.GetContent<EmailContent>();
    content.Attachments.Add(File.ReadAllBytes("sample.msg"), "sample.msg");

    // Save changes
    watermarker.Save("message.msg");
}