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"
Watermarkerwatermarker=newWatermarker("message.msg",loadOptions);EmailContentcontent=watermarker.getContent(EmailContent.class);// Set the plain text body
content.setBody("Test plain text body");// Set the html body
content.setHtmlBody("<html><body>Test html body</body></html>");// Set the email subject
content.setSubject("Test subject");// Save changes
watermarker.save("message.msg");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("message.msg",loadOptions);EmailContentcontent=watermarker.getContent(EmailContent.class);FileimageFile=newFile("sample.jpg");byte[]imageBytes=newbyte[(int)imageFile.length()];InputStreamimageInputStream=newFileInputStream(imageFile);imageInputStream.read(imageBytes);imageInputStream.close();content.getEmbeddedObjects().add(imageBytes,"sample.jpg");EmailEmbeddedObjectembeddedObject=content.getEmbeddedObjects().get_Item(content.getEmbeddedObjects().getCount()-1);content.setHtmlBody("<html><body>This is an embedded image: <img src=\"cid:"+embeddedObject.getContentId()+"\"></body></html>");watermarker.save("message.msg");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("message.msg",loadOptions);EmailContentcontent=watermarker.getContent(EmailContent.class);for(inti=content.getEmbeddedObjects().getCount()-1;i>=0;i--){if(content.getEmbeddedObjects().get_Item(i).getDocumentInfo().getFileType()==FileType.JPEG){// Remove reference to the image from html body
Stringpattern="<img[^>]*src=\"cid:"+content.getEmbeddedObjects().get_Item(i).getContentId()+"\"[^>]*>";content.setHtmlBody(content.getHtmlBody().replaceAll(pattern,""));// Remove the image
content.getEmbeddedObjects().removeAt(i);}}watermarker.save("message.msg");watermarker.close();
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 Email Message:
EmailLoadOptionsloadOptions=newEmailLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\message.msg"
Watermarkerwatermarker=newWatermarker("message.msg",loadOptions);SearchCriteriacriteria=newTextSearchCriteria("test",false);// Specify search locations
watermarker.getSearchableObjects().setEmailSearchableObjects(EmailSearchableObjects.Subject|EmailSearchableObjects.HtmlBody|EmailSearchableObjects.PlainTextBody);// Note, search is performed only if you pass TextSearchCriteria instance to FindWatermarks method
PossibleWatermarkCollectionwatermarks=watermarker.search(criteria);// Remove found text fragments
watermarks.clear();// Save changes
watermarker.save("message.msg");watermarker.close();
Listing all message recipients
GroupDocs.Watermark also allows listing all the message recipients using methods getTo(), getCc() and getBcc() as shown in the following code sample.
EmailLoadOptionsloadOptions=newEmailLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\message.msg"
Watermarkerwatermarker=newWatermarker("message.msg",loadOptions);EmailContentcontent=watermarker.getContent(EmailContent.class);// List all direct recipients
for(EmailAddressaddress:content.getTo()){System.out.println(address.getAddress());}// List all CC recipients
for(EmailAddressaddress:content.getCc()){System.out.println(address.getAddress());}// List all BCC recipients
for(EmailAddressaddress:content.getBcc()){System.out.println(address.getAddress());}watermarker.close();
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.