Learn how to add watermark in Outlook email messages
Modifying body and subject of email message
GroupDocs.Watermark also allows you to modify the body and subject of an email message. Below is the code sample to modify body and subject of an email Message:
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>();// Set the plain text bodycontent.Body="Test plain text body";// Set the html bodycontent.HtmlBody="<html><body>Test html body</body></html>";// Set the email subjectcontent.Subject="Test subject";// Save changeswatermarker.Save("message.msg");}
Embedding image to email message body
GroupDocs.Watermark also provides the feature of embedding images in the body of the email message. Following is the code sample for embedding an image in an 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>();content.EmbeddedObjects.Add(File.ReadAllBytes("sample.jpg"),"sample.jpg");EmailEmbeddedObjectembeddedObject=content.EmbeddedObjects[content.EmbeddedObjects.Count-1];content.HtmlBody=string.Format("<html><body>This is an embedded image: <img src=\"cid:{0}\"></body></html>",embeddedObject.ContentId);watermarker.Save("message.msg");}
Removing all embedded images from email message body
You can also remove the embedded images from the body of the email message. Below is the code for removing embedded images:
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.EmbeddedObjects.Count-1;i>=0;i--){if(content.EmbeddedObjects[i].GetDocumentInfo().FileType==FileType.JPEG){// Remove reference to the image from html bodystringpattern=string.Format("<img[^>]*src=\"cid:{0}\"[^>]*>",content.EmbeddedObjects[i].ContentId);content.HtmlBody=Regex.Replace(content.HtmlBody,pattern,string.Empty);// Remove the imagecontent.EmbeddedObjects.RemoveAt(i);}}watermarker.Save("message.msg");}
Searching text in email message body or subject
Using GroupDocs.Watermark, you can also search for a text in the subject as well as in the body of the email message. Following is the code sample for searching text in the email message:
EmailLoadOptionsloadOptions=newEmailLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\message.msg"using(Watermarkerwatermarker=newWatermarker("message.msg",loadOptions)){SearchCriteriacriteria=newTextSearchCriteria("test",false);// Specify search locationswatermarker.SearchableObjects.EmailSearchableObjects=EmailSearchableObjects.Subject|EmailSearchableObjects.HtmlBody|EmailSearchableObjects.PlainTextBody;// Note, search is performed only if you pass TextSearchCriteria instance to FindWatermarks methodPossibleWatermarkCollectionwatermarks=watermarker.Search(criteria);// Remove found text fragmentswatermarks.Clear();// Save changeswatermarker.Save("message.msg");}
Listing all message recipients
GroupDocs.Watermark also allows listing all the message recipients using properties To, Cc and Bcc as shown in the following code sample.
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>();// List all direct recipientsforeach(EmailAddressaddressincontent.To){Console.WriteLine(address.Address);}// List all CC recipientsforeach(EmailAddressaddressincontent.Cc){Console.WriteLine(address.Address);}// List all BCC recipientsforeach(EmailAddressaddressincontent.Bcc){Console.WriteLine(address.Address);}}
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.