-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form1.cs
145 lines (120 loc) · 5.44 KB
/
Form1.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using DevExpress.XtraRichEdit;
using DevExpress.Utils;
using DevExpress.Office.Services;
using System.Net.Mime;
using System.IO;
using DevExpress.XtraRichEdit.Export;
using DevExpress.XtraRichEdit.Export.Html;
using System.Net;
using DevExpress.Office.Utils;
namespace RichEditSendMail {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
richEdit.LoadDocument("Hello.docx");
}
private void btnSend_Click(object sender, EventArgs e) {
if ((edtTo.Text.Trim() == "") || (edtSubject.Text.Trim() == "") || (edtSmtp.Text.Trim() == ""))
{
MessageBox.Show("Fill in required fields");
return;
}
try
{
MailMessage mailMessage = new MailMessage("XtraRichEdit@devexpress.com", edtTo.Text);
mailMessage.Subject = edtSubject.Text;
RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(richEdit, mailMessage);
exporter.Export();
SmtpClient mailSender = new SmtpClient(edtSmtp.Text);
//specify your login/password to log on to the SMTP server, if required
//mailSender.Credentials = new NetworkCredential("login", "password");
mailSender.Send(mailMessage);
MessageBox.Show("Message sent", "RichEditSendMail", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exc) {
MessageBox.Show(exc.Message);
}
}
public class RichEditMailMessageExporter : IUriProvider {
readonly RichEditControl control;
readonly MailMessage message;
List<AttachementInfo> attachments;
int imageId;
public RichEditMailMessageExporter(RichEditControl control, MailMessage message) {
Guard.ArgumentNotNull(control, "control");
Guard.ArgumentNotNull(message, "message");
this.control = control;
this.message = message;
}
public virtual void Export() {
this.attachments = new List<AttachementInfo>();
AlternateView htmlView = CreateHtmlView();
message.AlternateViews.Add(htmlView);
message.IsBodyHtml = true;
}
protected internal virtual AlternateView CreateHtmlView() {
control.BeforeExport += OnBeforeExport;
string htmlBody = control.Document.GetHtmlText(control.Document.Range, this);
AlternateView view = AlternateView.CreateAlternateViewFromString(htmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
control.BeforeExport -= OnBeforeExport;
int count = attachments.Count;
for (int i = 0; i < count; i++) {
AttachementInfo info = attachments[i];
LinkedResource resource = new LinkedResource(info.Stream, info.MimeType);
resource.ContentId = info.ContentId;
view.LinkedResources.Add(resource);
}
return view;
}
void OnBeforeExport(object sender, BeforeExportEventArgs e) {
HtmlDocumentExporterOptions options = e.Options as HtmlDocumentExporterOptions;
if (options != null) {
options.Encoding = Encoding.UTF8;
}
}
#region IUriProvider Members
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}", imageId);
imageId++;
OfficeImageFormat imageFormat = GetActualImageFormat(image.RawFormat);
Stream stream = new MemoryStream(image.GetImageBytes(imageFormat));
string mediaContentType = OfficeImage.GetContentType(imageFormat);
AttachementInfo info = new AttachementInfo(stream, mediaContentType, imageName);
attachments.Add(info);
return "cid:" + imageName;
}
OfficeImageFormat GetActualImageFormat(OfficeImageFormat _officeImageFormat) {
if (_officeImageFormat == OfficeImageFormat.Exif ||
_officeImageFormat == OfficeImageFormat.MemoryBmp)
return OfficeImageFormat.Png;
else
return _officeImageFormat;
}
#endregion
}
public class AttachementInfo {
Stream stream;
string mimeType;
string contentId;
public AttachementInfo(Stream stream, string mimeType, string contentId) {
this.stream = stream;
this.mimeType = mimeType;
this.contentId = contentId;
}
public Stream Stream { get { return stream; } }
public string MimeType { get { return mimeType; } }
public string ContentId { get { return contentId; } }
}
}
}