|
Embedding Images and Sending Email
One of the most common usage of this editor control is building an email message
composer.
From Version 4.0.1.0, feature for embedding local images and sending as an email
has been implemented and it is just a single method call. Embedding Images that
can be viewed by Browsers can be done by calling the method EmbedImages().
htmlEditor1.EmbedImages();
This method will convert all the local images as a base64 string and update the
<img tags of the DocumentHtml. Please note, this Image Embedding is not good for
Email Message. If you call this method, your DocumentHtml will be changed and
you will not get your old html back. So, be careful when you use this method.
Once you call this method, the html will be updated and it will be portable.
That means, you can save the html in a database and retrieve from database to
show the image in any Browser that supports base64 embedded images. But again,
"not good for Email Message"
Then, what is good for Email Message ? Well, different email sending library
handles embedded images differently. Some of them make those images as
attachment and call those attachments as related attachment and some of them
does it differently. So, if we provided a method for converting the images for
Email Message, that wont make any sense as it will depend on which Email Sending
library you use. So, we just integrated
DotNetOpenMail
library with this control and
DotNetOpenMail is a very good widely accepted Free library for SMTP client
development. Then, your job is 99% done. Now, guess what. We have added a set of
overloaded methods for sending emails and if you call those methods, this
control will embed all the local images and make them related attachment and
create appropriate email message and send to your preferred receipent using your
provided SMTP credentials. We really did not want you to spend time on learning
our API, so we made many overloaded methods. You just browse through the methods
using the Visual Studio Intellisense and findout which method works best for
you. Here is the illustration.
By the way,
the following methods are available under the composite property named
EmailFacility. We will show some example snippets at the end.
Overload 1
|
C# |
public bool SendEmail(
string fromName,
string fromEmailAddress,
string toName,
string toEmailAddress,
string subject,
string attachmentFilePath,
string attachmentContentType,
string smtpServerName,
int smtpPort,
string smtpAccUserName,
string smtpAccPassword)
|
|
Overload 2
|
Overload 3:
|
Overload 4:
|
Overload 5:
public bool SendEmail(
string fromName,
string fromEmailAddress,
string toName,
string toEmailAddress,
string subject,
string smtpServerName,
int smtpPort
)
|
Overload 6:
|
Overload 7:
|
|
|
So, if you have all the common SMTP settings without any special customization,
then, you may use overload 1. You might be wondering, what is the type EmailMessage in Overload 7.
Ok, EmailMessage is the type defined in DotNetOpenMail Namespace but this
control gives you a method for getting an object of EmailMessage which will
contian the Editor's DocumentHtml where all the local images are embedded as a
related attachment. This method comes with 2 overloads. What this method
does is, it takes the email related information like, from name, from email
address, to name, to email address, subject etc and give you an object of
EmailMessage which contains the local images embedded as related attachments.
Here are 2 overload illustration.
C#
public EmailMessage GetEmailMessage(
string fromName,
string fromEmailAddress,
string toName,
string toEmailAddress,
string subject,
string attachmentFilePath,
string attachmentContentType
) |
C#
public EmailMessage GetEmailMessage(
string fromName,
string fromEmailAddress,
string toName,
string toEmailAddress,
string subject
) |
One overload will let you add one additional file attachment. In order to make
it simple and not complicated, we made the overload for a single extra
attachment file. But if you want to add more extra attachment, you can do that
too. The example snippet is provided at the end of this page.
Ok, so far, you found all the overloads in all methods missing one thing. That
is EmailBody, right ? The email body is not need to be provided as the email
body is the content of the editor, is not it ? Yes, then, the control knows
about the content and it will do accordingly.
You may need to use the GetEmailMessage(..) methods only if you are using
Overload 7 for sending email. Otherwise you wont need it.
Now, lets show you some snippets of using the Email Sending feature.
C#
private void button2_Click(object sender, EventArgs e)
{
bool result = htmlEditor1.EmailFacility.SendEmail("From Name", "support@spicelogic.com",
"To Name", "spicelogic@gmail.com", "THIS IS THE EMAIL SUBJECT",
"YOUR SMTP ADDRESS (i.e. smtp.gmail.com)",
25, "SMTP USER NAME", "SMTP PASSWORD");
if (result)
MessageBox.Show("success");
else
MessageBox.Show("fail");
}
VB.NET
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim result As Boolean = htmlEditor1.EmailFacility.SendEmail("From Name", "support@spicelogic.com", "To Name", "spicelogic@gmail.com", "THIS IS THE EMAIL SUBJECT", "YOUR SMTP ADDRESS (i.e. smtp.gmail.com)", _
25, "SMTP USER NAME", "SMTP PASSWORD")
If result Then
MessageBox.Show("success")
Else
MessageBox.Show("fail")
End If
End Sub
Now, The following snippet will show how you can add more
attachments in addition to the embedded images which were automatically added as
related attachments:
C#
private void button2_Click(object sender, EventArgs e)
{
var myEmailMessage = htmlEditor1.EmailFacility.GetEmailMessage("Mike John", "spicelogic@xxxx.com", "Line Mohn", "line@mohn.com",
"This is the Subject");
myEmailMessage.AddMixedAttachment(new DotNetOpenMail.FileAttachment(new FileInfo("c:\\temp\\myPicture.jpg")));
myEmailMessage.AddMixedAttachment(new DotNetOpenMail.FileAttachment(new FileInfo("c:\\temp\\yourPicture.png")));
bool result = htmlEditor1.EmailFacility.SendEmail(myEmailMessage, new DotNetOpenMail.SmtpServer("smtp.myserver.com", 25));
if (result)
MessageBox.Show("success");
else
MessageBox.Show("fail");
}
|
|
|
|
|