Click here to Skip to main content
15,891,372 members
Articles / Desktop Programming / Windows Forms

E-mail Sender with HTML Editor & Quick Drawer

Rate me:
Please Sign up or sign in to vote.
3.89/5 (4 votes)
11 Nov 2009CPOL1 min read 323.8K   1.1K   38   15
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)


Written By
Software Developer
Egypt Egypt
- BSc Computer Engineering
Ain Shams University - Faculty of Engineering

Comments and Discussions

 
QuestionHow do I change the font, color and size of the text only for the selected text and convert it to html? Pin
Sivajish16-Nov-09 12:09
Sivajish16-Nov-09 12:09 
Hi,

This is a nice article.

Is it possible to format only part of the text (by changing the size, font) and have an HTML output for the same? Please let me know.

Appreciate your help.

Thanks.
AnswerRe: How do I change the font, color and size of the text only for the selected text and convert it to html? Pin
AntounPG16-Nov-09 12:22
AntounPG16-Nov-09 12:22 
GeneralRe: How do I change the font, color and size of the text only for the selected text and convert it to html? Pin
Sivajish17-Nov-09 4:35
Sivajish17-Nov-09 4:35 
GeneralSugestion / Question... Pin
Fiwel11-Nov-09 6:57
Fiwel11-Nov-09 6:57 
GeneralRe: Sugestion / Question... Pin
AntounPG11-Nov-09 9:48
AntounPG11-Nov-09 9:48 
GeneralGood but could be better. Pin
Abhishek Sur3-Nov-09 4:33
professionalAbhishek Sur3-Nov-09 4:33 
GeneralRe: Good but could be better. Pin
AntounPG3-Nov-09 5:51
AntounPG3-Nov-09 5:51 
GeneralRe: Good but could be better. Pin
Abhishek Sur3-Nov-09 6:03
professionalAbhishek Sur3-Nov-09 6:03 
GeneralRe: Good but could be better. Pin
AntounPG11-Nov-09 21:01
AntounPG11-Nov-09 21:01 
GeneralRe: Good but could be better. Pin
Abhishek Sur11-Nov-09 21:05
professionalAbhishek Sur11-Nov-09 21:05 
GeneralMy vote of 2 Pin
SmirkinGherkin3-Nov-09 1:05
SmirkinGherkin3-Nov-09 1:05 
GeneralRe: My vote of 2 Pin
AntounPG3-Nov-09 5:48
AntounPG3-Nov-09 5:48 
GeneralRe: My vote of 2 Pin
SmirkinGherkin4-Nov-09 1:38
SmirkinGherkin4-Nov-09 1:38 
GeneralRe: My vote of 2 Pin
AntounPG4-Nov-09 1:51
AntounPG4-Nov-09 1:51 
GeneralRe: My vote of 2 Pin
AntounPG11-Nov-09 21:00
AntounPG11-Nov-09 21:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.