This example illustrates how the built-in document export functionality of the XtraRichEdit can be used to construct a simple application allowing you to send the document via email. A document, loaded into the RichEditControl, is sent in HTML email format.
To accomplish the task, we have to transform the document into the HTML formatted stream. Inline pictures will form a collection of linked resources (System.Net.Mail.LinkedResourceCollection ) for the email attachment. Common methods of the System.Net.Mail.MailMessage class are used to create a message. The message is sent with the help of the System.Net.Mail.SmtpClient class instance.
Let's elaborate a document export process further. To gain a control over RichEdit export, we construct a RichEditMailMessageExporter class, which implements the IUriProvider interface. This interface contains two methods - CreateCssUri and CreateImageUri. We use default CSS handling, so the CreateCssUri method always returns null. The CreateImageUri method is used to transform each document image into an object of the helper class - the AttachmentInfo class instance. An instance of this class contains an image's name, type and the data stream. An image is identified by its name, so this method returns a CID (Content-ID) URL containing the image name, to include a link to the image in the message body.
The CreateHtmlView method of the RichEditMailMessageExporter gets the content of the RichEdit document using the GetHtmlText method, handles the BeforeExport event to specify encoding and creates the System.Net.Mail.AlternateView object required for HTML email format. Note that the document's HTML code contains CID URIs constructed using the CreateImageUri method in place of images. Then a collection of AttachmentInfo objects, representing document images, is used to fill a collection of embedded resources for the message.
The Export method of the RichEditMailMessageExporter class calls the CreateHtmlView method to accomplish the major task, and adjusts the message parameters as required.
Now, when the main functionality is incorporated into the RichEditMailMessageExporter class, the code for creating and sending the message looks simple and straightforward. Create a message, fill in 'from' and 'to' fields, create an exporter and call its 'Export' method, create an smtp client instance, specify the E-mail account settings and call the 'Send' method to send a message.
Question Comments
Added By: Darrin Lynn at: 11/1/2012 9:28:49 AM
Very useful sample and works well with Outlook. However when sent to my iPhone with an image in the body it is not rendered correctly. Do you have any experience of this?
Added By: Panayiotis Anastasiou at: 12/13/2012 1:23:42 AMDear sirs i have also faced that problem and tried it.
It works but when sent to android device i have problems rendering images.
Any solutions for that?
I need to do this same thing too but I am using Exchange Web Services to send the email. In this example MailMessage is being used but I am using the object EmailMessage from Microsoft.Exchange.Webservices. Can this be done with the object EmailMessage?
Added By: Brendon Muck [DevExpress MVP] at: 3/11/2016 7:17:40 AM There are several documentation links in this ticket that no longer resolve correctly to their respective documentation topics:IUriProvider
CreateCssUri
CreateImageUri.
GetHtmlText