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();
// Constants.InMessageMsg is an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"
using (Watermarker watermarker = new Watermarker(Constants.InMessageMsg, 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(Constants.OutputPath, 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();
// Constants.InMessageMsg is an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"
using (Watermarker watermarker = new Watermarker(Constants.InMessageMsg, 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(Constants.OutMessageMsg);
}

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();
// Constants.InMessageMsg is an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"
using (Watermarker watermarker = new Watermarker(Constants.InMessageMsg, loadOptions))
{
    EmailContent content = watermarker.GetContent<EmailContent>();
    content.Attachments.Add(File.ReadAllBytes(Constants.InSampleMsg), "sample.msg");

    // Save changes
    watermarker.Save(Constants.OutMessageMsg);
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document watermarking App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to add watermark to PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Watermarking App.