Introduction
I am trying to introduce something new here with some funny features, while using the System.Net.Mail
. Like:
- Send an e-mail with a built in HTML editor which can send special formatted e-mails.
- Draw your own paintings, save and/or attach them immediately with one click.
- Contacts List
- 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(';');
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);
sc.Credentials = new System.Net.NetworkCredential(mailSender.ToString(), paTxt.Text);
sc.EnableSsl = true;
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