65.9K
CodeProject 正在变化。 阅读更多。
Home

带 HTML 编辑器和快速绘图的电子邮件发送器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.89/5 (4投票s)

2009 年 11 月 3 日

CPOL

1分钟阅读

viewsIcon

325467

downloadIcon

1193

通过应用程序发送电子邮件,该应用程序具有 HTML 编辑器和一个绘图工具,可以绘制自己的附件并立即发送。

引言

我试图在这里引入一些新东西,并带有一些有趣的功能,同时使用 System.Net.Mail。例如

  1. 发送带有内置 HTML 编辑器的电子邮件,可以发送特殊格式的电子邮件。
  2. 绘制自己的绘画,保存和/或立即一键附加。
  3. 联系人列表
  4. 用户数据加密

Using the Code

主代码(初始化和发送电子邮件)

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;
}  	

加密

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();

解密

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();

HTML 编辑器:字体类型、大小和颜色

调用方法并发送某些标志以调整 UI 字体

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);
            }
        }

检查我自己的标志并将用户界面选择转换为 HTML 脚本。

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;

绘图面板

初始化一个 Windows Form,其中包含一个 PictureBox 用于绘图。

这里,我们有两个 Graphics:第一个用于在 PictureBox 上绘图,从而允许用户看到他正在绘制的内容,另一个是不可见的图形,它在 Bitmap 上绘图,以便保存绘图。

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

联系人列表

我向项目中添加了一个用户控件“ContactCard”,它代表联系人列表中的一个实例。

有关绘图类和联系人列表的更多信息,请查看附带的代码。

历史

  • 2009 年 11 月 3 日:首次发布
  • 2009 年 11 月 7 日:文章已更新
© . All rights reserved.