-
Notifications
You must be signed in to change notification settings - Fork 0
/
RichEditMailMessageExporter.cs
70 lines (58 loc) · 2.31 KB
/
RichEditMailMessageExporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Text;
using DevExpress.XtraRichEdit;
using DevExpress.Office.Services;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.IO;
using DevExpress.Utils;
using DevExpress.Office.Utils;
using System.Drawing.Imaging;
using DevExpress.XtraRichEdit.Export;
namespace MailMergeEmail
{
public class RichEditMailMessageExporter : IUriProvider
{
readonly RichEditDocumentServer server;
readonly Outlook.MailItem mailItem;
public RichEditMailMessageExporter(RichEditDocumentServer richServer, Outlook.MailItem mailItem)
{
Guard.ArgumentNotNull(richServer, "control");
Guard.ArgumentNotNull(mailItem, "mailItem");
this.server = richServer;
this.mailItem = mailItem;
}
string tempFiles = Path.Combine(Directory.GetCurrentDirectory(), "TempFiles");
public virtual void Export()
{
if (!Directory.Exists(tempFiles))
Directory.CreateDirectory(tempFiles);
server.BeforeExport += OnBeforeExport;
string htmlBody = server.Document.GetHtmlText(server.Document.Range, this);
server.BeforeExport -= OnBeforeExport;
mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mailItem.HTMLBody = htmlBody;
}
private void OnBeforeExport(object sender, BeforeExportEventArgs e)
{
HtmlDocumentExporterOptions options = e.Options as HtmlDocumentExporterOptions;
if (options != null)
{
options.Encoding = Encoding.UTF8;
}
}
int imageId;
public string CreateCssUri(string rootUri, string styleText, string relativeUri)
{
return String.Empty;
}
public string CreateImageUri(string rootUri, OfficeImage image, string relativeUri)
{
string imageName = String.Format("image{0}.png", imageId);
imageId++;
string imagePath = Path.Combine(tempFiles, imageName);
image.NativeImage.Save(imagePath, ImageFormat.Png);
mailItem.Attachments.Add(imagePath, Outlook.OlAttachmentType.olByValue, 0, Type.Missing);
return "cid:" + imageName;
}
}
}