Click here to Skip to main content
Click here to Skip to main content

E-mail Sender with HTML Editor & Quick Drawer

By , 11 Nov 2009
 

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)

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

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

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:

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.

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.

Bitmap bmp;
Graphics g1;
Graphics g2;
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)

About the Author

Antoun Gorgy
Software Developer
Egypt Egypt
Member
- BSc Computer Engineering
Ain Shams University - Faculty of Engineering

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow do I change the font, color and size of the text only for the selected text and convert it to html?memberSivajish16 Nov '09 - 12:09 
AnswerRe: How do I change the font, color and size of the text only for the selected text and convert it to html?memberAntoun Gorgy16 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?memberSivajish17 Nov '09 - 4:35 
GeneralSugestion / Question...memberFiwel11 Nov '09 - 6:57 
GeneralRe: Sugestion / Question...memberAntoun Gorgy11 Nov '09 - 9:48 
GeneralGood but could be better.memberAbhishek Sur3 Nov '09 - 4:33 
GeneralRe: Good but could be better.memberAntoun Gorgy3 Nov '09 - 5:51 
GeneralRe: Good but could be better.memberAbhishek Sur3 Nov '09 - 6:03 
GeneralRe: Good but could be better.memberAntoun Gorgy11 Nov '09 - 21:01 
GeneralRe: Good but could be better.memberAbhishek Sur11 Nov '09 - 21:05 
GeneralMy vote of 2memberSmirkinGherkin3 Nov '09 - 1:05 
GeneralRe: My vote of 2memberAntoun Gorgy3 Nov '09 - 5:48 
GeneralRe: My vote of 2memberSmirkinGherkin4 Nov '09 - 1:38 
GeneralRe: My vote of 2memberAntoun Gorgy4 Nov '09 - 1:51 
GeneralRe: My vote of 2memberAntoun Gorgy11 Nov '09 - 21:00 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 11 Nov 2009
Article Copyright 2009 by Antoun Gorgy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid