Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

E-mail Sender with HTML Editor & Quick Drawer

3.89/5 (4 votes)
11 Nov 2009CPOL1 min read 324.7K   1.2K  
Send E-mail via Application with an HTML editor and a drawer to draw your own attachments and send them immediately.

Introduction

I am trying to introduce something new here with some funny features, while using the System.Net.Mail. Like:

  1. Send an e-mail with a built in HTML editor which can send special formatted e-mails.
  2. Draw your own paintings, save and/or attach them immediately with one click.
  3. Contacts List
  4. User Data Encryption  

Using the Code

The Main Code (Initializing and Sending Emails)

C#
MailMessage myMail = new MailMessage();
MailAddress mailSender = new MailAddress(nameTxt.Text + "@gmail.com");
string[] addresses = to_txt.Text.Split(';'); //allows multiple receivers addresses
for (int i = 0; i < addresses.Length; i++)

	myMail.To.Add(addresses[i]);

myMail.From = mailSender;

myMail.Subject = subj_txt.Text;
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587); //initializing the SMTP 
						//client and port no.
//acquiring username and password
sc.Credentials = new System.Net.NetworkCredential(mailSender.ToString(), paTxt.Text);
sc.EnableSsl = true;//enable the SSL
try
{
      sc.Send(myMail);
}
catch (Exception ex)
{
      MessageBox.Show(ex.Message);
      return;
}  	

Encryption

C#
FileStream fsFileIn = File.OpenRead("C:\\myGmailer\\user.gmailer");
FileStream fsKeyFile = File.OpenRead("C:\\myGmailer\\params.gmailer");

TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
BinaryReader brFile = new BinaryReader(fsKeyFile);
cryptAlgorithm.Key = brFile.ReadBytes(24);
cryptAlgorithm.IV = brFile.ReadBytes(8);
fsKeyFile.Close();

CryptoStream csEncrypt = new CryptoStream
		(fsFileIn, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader srCleanStream = new StreamReader(csEncrypt);
nameTxt.Text = srCleanStream.ReadLine();
paTxt.Text = srCleanStream.ReadLine();
srCleanStream.Close();

Decryption

C#
FileStream fsFileOut = File.Create("C:\\myGmailer\\user.gmailer");

TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();

CryptoStream csEncrypt = new CryptoStream
		(fsFileOut, cryptAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);

StreamWriter swEncStream = new StreamWriter(csEncrypt);

swEncStream.WriteLine(nameTxt.Text);
swEncStream.WriteLine(paTxt.Text);
swEncStream.Flush();
swEncStream.Close();

BinaryWriter bwFile = new BinaryWriter(File.Create("C:\\myGmailer\\params.gmailer"));
bwFile.Write(cryptAlgorithm.Key);
bwFile.Write(cryptAlgorithm.IV);
bwFile.Flush();
bwFile.Close();

The HTML Editor: Font Type, Size, and Color

Method to call and send certain flags to adjust the UI font:

C#
private void AdjustFont(bool bold_, bool italic_, bool underlined_)
        {
            if (bold && !italic && !underlined)
            {
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Regular);
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Bold);
            }
            else if (!bold && italic && !underlined)
            {
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Regular);
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Italic);
            }
            else if (!bold && !italic && underlined)
            {
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Regular);
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Underline);
            }
            else if (bold && italic && !underlined)
            {
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Regular);
                rchtxtbx_body.Font = new Font
		(rchtxtbx_body.Font, FontStyle.Bold | FontStyle.Italic);
            }
            else if (bold && !italic && underlined)
            {
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Regular);
                rchtxtbx_body.Font = 
		new Font(rchtxtbx_body.Font, FontStyle.Bold | FontStyle.Underline);
            }
            else if (!bold && italic && underlined)
            {
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Regular);
                rchtxtbx_body.Font = 
		new Font(rchtxtbx_body.Font, FontStyle.Underline | FontStyle.Italic);
            }
            if (!bold && !italic && !underlined)
            {
                rchtxtbx_body.Font = new Font(rchtxtbx_body.Font, FontStyle.Regular);
            }
            else if (bold && italic && underlined)
            {
                rchtxtbx_body.Font = 
		new Font(rchtxtbx_body.Font, FontStyle.Underline | 
		FontStyle.Italic | FontStyle.Bold);
            }
        }

Checking my Own flags and translating the user interface choice to an HTML script.

C#
string body = rchtxtbx_body.Text;
if (bold)
                body = "<b>" + body + "</b>";
if(italic)
                body = "<i>" + body + "</i>";
if(underlined)
                body = "<u>" + body + "</u>";
if (comboBox1.SelectedItem.ToString() == "Large")
                body = "<font color=\""+ colorName +"\"size=\"5\">" + body + "</font>";
else if (comboBox1.SelectedItem.ToString() == "Medium")
                body = "<font color=\"" + colorName + "\"size=\"3\">" + body + "</font>";
else if (comboBox1.SelectedItem.ToString() == "Small")
                body = "<font color=\"" + colorName + "\"size=\"1\">" + body + "</font>";
myMail.Body = body;
myMail.IsBodyHtml = true;

Drawing Panel

Initializing a Windows Form with a picturebox to draw on it.

Here, we have two Graphics: The first one is to draw on picture box thus allowing the user to see what he is drawing, and the other is an invisible graphic which draws on a bitmap in order to save the drawing.

C#
Bitmap bmp;
Graphics g1;
Graphics g2;
C#
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g1 = Graphics.FromImage(bmp);
g2 = pictureBox1.CreateGraphics();

Contacts List

I added a user control to the project "ContactCard" which represents an instance in the contact List.

For more information about the drawing class, contact List, check the attached code.

History

  • 3rd November, 2009: Initial post
  • 7th November, 2009: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)