MainForm.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:22k
源码类别:

Email服务器

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Net.Mail;
  10. using System.Net;
  11. namespace myWorkPad
  12. {
  13.     public partial class MainForm : Form
  14.     {
  15.         private int checkPrint;
  16.         string strFileName = "";
  17.         string strName= "";
  18.         string strEmail = "";
  19.         string strUserName;
  20.         string strPass;
  21.         string strPOP3;
  22.         string strSMTP;
  23.         string strAttach = "";
  24.         public MainForm()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.         private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  29.         {
  30.             checkPrint = rtMain.Print(checkPrint, rtMain.TextLength, e);
  31.             // Check for more pages
  32.             if (checkPrint < rtMain.TextLength)
  33.                 e.HasMorePages = true;
  34.             else
  35.                 e.HasMorePages = false;
  36.         }
  37.         private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
  38.         {
  39.             checkPrint = 0;
  40.         }
  41.         private void printDocument_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
  42.         {
  43.             MessageBox.Show(printDocument.DocumentName + " 已经打印完成!");
  44.         }
  45.         private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
  46.         {
  47.             OpenFileDialog openFileDialog = new OpenFileDialog();
  48.             openFileDialog.InitialDirectory = "c:\";//注意这里写路径时要用c:\而不是c:
  49.             openFileDialog.Filter = "文本文件|*.txt|RTF文件|*.rtf|html文件|*.html|所有文件|*.*";
  50.             openFileDialog.RestoreDirectory = true;
  51.             openFileDialog.FilterIndex = 2;
  52.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  53.             {
  54.                 try
  55.                 {
  56.                     string fName = openFileDialog.FileName;
  57.                     string strExt = System.IO.Path.GetExtension(fName).ToLower();
  58.                     if (strExt == ".rtf")
  59.                     {
  60.                         rtMain.LoadFile(fName, RichTextBoxStreamType.RichText);
  61.                     }
  62.                     else if (strExt == ".txt")
  63.                     {
  64.                         rtMain.LoadFile(fName, RichTextBoxStreamType.PlainText);
  65.                     }
  66.                     else if ((strExt == ".htm") || (strExt == ".html"))
  67.                     {
  68.                         // Read the HTML format
  69.                         StreamReader sr = File.OpenText(fName);
  70.                         string strHTML = sr.ReadToEnd();
  71.                         sr.Close();
  72.                         rtMain.AddHTML(strHTML);
  73.                     }                  
  74.                     printDocument.DocumentName = Path.GetFileName(fName);
  75.                     strFileName = fName;
  76.                     this.Text = Path.GetFileNameWithoutExtension(fName) +"--邮件";  
  77.                 }
  78.                 catch (System.IO.FileNotFoundException)
  79.                 {
  80.                     MessageBox.Show("没有找到文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  81.                 }
  82.             }
  83.             rtMain.Focus();
  84.         }
  85.         private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
  86.         {
  87.             SaveFileDialog saveFileDialog = new SaveFileDialog();
  88.             saveFileDialog.Filter = "文本文件|*.txt|RTF文件|*.rtf|html文件|*.html|所有文件|*.*";
  89.             saveFileDialog.FilterIndex = 2;
  90.             saveFileDialog.RestoreDirectory = true;
  91.             if (saveFileDialog.ShowDialog() == DialogResult.OK)
  92.             {
  93.                 try
  94.                 {
  95.                     string strName = saveFileDialog.FileName;
  96.                     switch (Path.GetExtension(strName).ToLower())
  97.                     {
  98.                         case ".txt":
  99.                             rtMain.SaveFile(strName, RichTextBoxStreamType.PlainText);
  100.                             break;
  101.                         case ".rtf":
  102.                             rtMain.SaveFile(strName, RichTextBoxStreamType.RichText);
  103.                             break;
  104.                         case ".html":
  105.                             {
  106.                                 try
  107.                                 {
  108.                                     // save as HTML format
  109.                                     string strText = rtMain.GetHTML(true, true);
  110.                                     if (File.Exists(strName))
  111.                                         File.Delete(strName);
  112.                                     StreamWriter sr = File.CreateText(strName);
  113.                                     sr.Write(strText);
  114.                                     sr.Close();
  115.                                 }
  116.                                 catch
  117.                                 {
  118.                                     MessageBox.Show("无法保存文件!");
  119.                                 }
  120.                                 break;
  121.                             }
  122.                             
  123.                         default:
  124.                             rtMain.SaveFile(strName);
  125.                             break;
  126.                     }
  127.                 }
  128.                 catch (System.Exception err)
  129.                 {
  130.                     MessageBox.Show(err.Message);
  131.                 }
  132.             }
  133.             rtMain.Focus();
  134.         }
  135.         private void toolStripMenuItem1_Click(object sender, EventArgs e)
  136.         {
  137.             PageSetupDialog pageSetupDialog = new PageSetupDialog();
  138.             pageSetupDialog.Document = printDocument;
  139.             pageSetupDialog.ShowDialog();
  140.         }
  141.         private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  142.         {
  143.             PrintDialog printDialog = new PrintDialog();
  144.             printDialog.Document = printDocument;
  145.             if (printDialog.ShowDialog() != DialogResult.Cancel)
  146.             {
  147.                 try
  148.                 {
  149.                     printDocument.Print();
  150.                 }
  151.                 catch (Exception ex)
  152.                 {
  153.                     MessageBox.Show(ex.Message);
  154.                 }
  155.             }
  156.         }
  157.         private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
  158.         {
  159.             StringReader lineReader = new StringReader(rtMain.Text);
  160.             try
  161.             {
  162.                 PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
  163.                 printPreviewDialog.Document = printDocument;
  164.                 printPreviewDialog.FormBorderStyle = FormBorderStyle.Fixed3D;
  165.                 printPreviewDialog.ShowDialog();
  166.             }
  167.             catch (Exception excep)
  168.             {
  169.                 MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
  170.                 return;
  171.             }
  172.         }
  173.         private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  174.         {
  175.             Application.Exit();
  176.         }
  177.         private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
  178.         {
  179.             rtMain.Undo();
  180.         }
  181.         private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
  182.         {
  183.             rtMain.Redo();
  184.         }
  185.         private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
  186.         {
  187.             rtMain.Cut();
  188.         }
  189.         private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
  190.         {
  191.             rtMain.Copy();
  192.         }
  193.         private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
  194.         {
  195.             rtMain.Paste();
  196.         }
  197.         private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
  198.         {
  199.             rtMain.SelectAll();
  200.         }
  201.         private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
  202.         {
  203.             rtMain.Clear();
  204.         }
  205.         private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
  206.         {
  207.             FontDialog fontDialog = new FontDialog();
  208.             fontDialog.Color = rtMain.ForeColor;
  209.             fontDialog.AllowScriptChange = true;
  210.             //fontDialog.ShowColor = true;
  211.             if (fontDialog.ShowDialog() != DialogResult.Cancel)
  212.             {
  213.                 rtMain.SelectionFont = fontDialog.Font;//将当前选定的文字改变字体
  214.                 // rtMain.SelectionColor = fontDialog.Color;
  215.             }
  216.             rtMain.Focus();
  217.         }
  218.         private void 颜色CToolStripMenuItem_Click(object sender, EventArgs e)
  219.         {
  220.             ColorDialog colorDialog = new ColorDialog();
  221.             colorDialog.AllowFullOpen = true;
  222.             colorDialog.FullOpen = true;
  223.             colorDialog.ShowHelp = true;
  224.             colorDialog.Color = Color.Black;//初始化当前文本框中的字体颜色,当用户在ColorDialog对话框中点击"取消"按钮恢复原来的值
  225.             if (colorDialog.ShowDialog() != DialogResult.Cancel)
  226.             {
  227.                 rtMain.SelectionColor = colorDialog.Color; //将当前选定的文字改变颜色
  228.             }
  229.             rtMain.Focus();
  230.         }
  231.         private void 图像PToolStripMenuItem_Click(object sender, EventArgs e)
  232.         {
  233.             OpenFileDialog openFileDialog = new OpenFileDialog();
  234.             // openFileDialog.InitialDirectory = "c:\";//注意这里写路径时要用c:\而不是c:
  235.             openFileDialog.Filter = "位图文件|*.bmp|gif文件|*.gif|TIFF文件|*.tif|jpg文件|*.jpg|所有文件|*.*";
  236.             openFileDialog.RestoreDirectory = true;
  237.             openFileDialog.FilterIndex = 1;
  238.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  239.             {
  240.                 try
  241.                 {
  242.                     string fName = openFileDialog.FileName;
  243.                     Image img;
  244.                     img = Image.FromFile(fName);
  245.                     Clipboard.SetDataObject(img);
  246.                     DataFormats.Format df;
  247.                     df = DataFormats.GetFormat(DataFormats.Bitmap);
  248.                     if (this.rtMain.CanPaste(df))
  249.                     {
  250.                         this.rtMain.Paste(df);
  251.                     }
  252.                 }
  253.                 catch (System.IO.FileNotFoundException)
  254.                 {
  255.                     MessageBox.Show("没有找到文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  256.                 }
  257.             }
  258.             rtMain.Focus();
  259.         }
  260.         private void 左对齐LToolStripMenuItem_Click(object sender, EventArgs e)
  261.         {
  262.             rtMain.SelectionAlignment = HorizontalAlignment.Left;
  263.         }
  264.         private void 居中EToolStripMenuItem_Click(object sender, EventArgs e)
  265.         {
  266.             rtMain.SelectionAlignment = HorizontalAlignment.Center;
  267.         }
  268.         private void 右对齐RToolStripMenuItem_Click(object sender, EventArgs e)
  269.         {
  270.             rtMain.SelectionAlignment = HorizontalAlignment.Right;
  271.         }
  272.         private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
  273.         {
  274.             AboutDialog dlg = new AboutDialog();
  275.             dlg.ShowDialog();
  276.         }
  277.         private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
  278.         {
  279.             if (strFileName != "")
  280.             {
  281.                 try
  282.                 {
  283.                     switch (Path.GetExtension(strFileName).ToLower())
  284.                     {
  285.                         case ".txt":
  286.                             rtMain.SaveFile(strFileName, RichTextBoxStreamType.PlainText);
  287.                             break;
  288.                         case ".rtf":
  289.                             rtMain.SaveFile(strFileName, RichTextBoxStreamType.RichText);
  290.                             break;
  291.                         case ".html":
  292. {
  293. try
  294. {
  295. // save as HTML format
  296.                                 string strText = rtMain.GetHTML(true, true);
  297.                                 if (File.Exists(strFileName))
  298.                                     File.Delete(strFileName);
  299.                                 StreamWriter sr = File.CreateText(strFileName);
  300. sr.Write(strText);
  301. sr.Close();
  302. }
  303. catch
  304. {
  305. MessageBox.Show("无法保存文件!");
  306. }
  307.                             break;
  308. }
  309.                         default:
  310.                             rtMain.SaveFile(strFileName);
  311.                             break;
  312.                     }
  313.                 }
  314.                 catch (System.Exception err)
  315.                 {
  316.                     MessageBox.Show(err.Message);
  317.                 }
  318.             }
  319.             else
  320.                 另存为AToolStripMenuItem_Click(sender, e);
  321.         }
  322.         private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  323.         {
  324.             if (MessageBox.Show("要保存现有文件吗", "提示对话框", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
  325.                 == DialogResult.Yes)
  326.                 保存SToolStripMenuItem_Click(sender, e);
  327.             else
  328.             {
  329.                 strFileName = "";
  330.                 rtMain.Clear();
  331.             }
  332.             this.Text = "新建文档";  
  333.         }
  334.         private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
  335.         {
  336.             ToolStripMenuItem ts = (ToolStripMenuItem)sender;
  337.             ts.Checked = !(ts.Checked);
  338.             toolMain.Visible = ts.Checked;
  339.         }
  340.         private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
  341.         {
  342.             ToolStripMenuItem ts = (ToolStripMenuItem)sender;
  343.             ts.Checked = !(ts.Checked);
  344.             statusMain.Visible = ts.Checked;
  345.         }
  346.     
  347.         private void 格式栏FToolStripMenuItem_Click(object sender, EventArgs e)
  348.         {
  349.             ToolStripMenuItem ts = (ToolStripMenuItem)sender;
  350.             ts.Checked = !(ts.Checked);
  351.             toolFormat.Visible = ts.Checked;
  352.         }
  353.         private void tsbBold_Click(object sender, EventArgs e)
  354.         {
  355.             //处理粗体显示
  356.             System.Drawing.Font oldFont = rtMain.SelectionFont;
  357.             System.Drawing.Font newFont;
  358.             if (oldFont.Bold)
  359.                 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
  360.             else
  361.                 newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
  362.             rtMain.SelectionFont = newFont;
  363.             rtMain.Focus();
  364.         }
  365.         private void ttsbItalic_Click(object sender, EventArgs e)
  366.         {
  367.             //处理斜体显示
  368.             System.Drawing.Font oldFont = rtMain.SelectionFont;
  369.             System.Drawing.Font newFont;
  370.             if (oldFont.Italic)
  371.                 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
  372.             else
  373.                 newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
  374.             rtMain.SelectionFont = newFont;
  375.             rtMain.Focus();
  376.         }
  377.         private void ttsbUnderLine_Click(object sender, EventArgs e)
  378.         {
  379.             //处理下划线显示
  380.             System.Drawing.Font oldFont = rtMain.SelectionFont; ;
  381. System.Drawing.Font newFont;
  382. if(oldFont.Underline)
  383. newFont = new Font(oldFont,oldFont.Style&~FontStyle.Underline);
  384. else
  385. newFont = new Font(oldFont,oldFont.Style|FontStyle.Underline);
  386.   rtMain.SelectionFont = newFont;
  387.             rtMain.Focus();
  388.         }
  389.         private void tsbBold_MouseEnter(object sender, EventArgs e)
  390.         {
  391.             statusLabel.Text = "将选定内容变为粗体";
  392.         }
  393.         private void tsbBold_MouseLeave(object sender, EventArgs e)
  394.         {
  395.             statusLabel.Text = "邮件发送系统1.0";
  396.         }
  397.         private void ttsbItalic_MouseEnter(object sender, EventArgs e)
  398.         {
  399.             statusLabel.Text = "将选定内容变为斜体";
  400.         }
  401.         private void ttsbUnderLine_MouseEnter(object sender, EventArgs e)
  402.         {
  403.             statusLabel.Text = "用连续下划线格式化选定内容";
  404.         }
  405.         private void 新建NToolStripButton_MouseEnter(object sender, EventArgs e)
  406.         {
  407.             statusLabel.Text = "创建新文档";
  408.         }
  409.         private void 打开OToolStripButton_MouseEnter(object sender, EventArgs e)
  410.         {
  411.             statusLabel.Text = "打开已存文档";
  412.         }
  413.         private void 保存SToolStripButton_MouseEnter(object sender, EventArgs e)
  414.         {
  415.             statusLabel.Text = "保存当前文档";
  416.         }
  417.         private void 打印PToolStripButton_MouseEnter(object sender, EventArgs e)
  418.         {
  419.             statusLabel.Text = "打印当前文档";
  420.         }
  421.         private void MainForm_Load(object sender, EventArgs e)
  422.         {
  423.             //初始化字体选择
  424.             foreach (FontFamily oneFontFamily in FontFamily.Families)
  425.             {
  426.                 tscbFont.Items.Add(oneFontFamily.Name);
  427.             }
  428.             tscbFont.SelectedItem = "宋体";
  429.             tscbSize.SelectedIndex = 2;
  430.             cbFormat.SelectedIndex = 0;
  431.             cbPriority.SelectedIndex = 0;
  432.         }
  433.         private void tscbFont_SelectedIndexChanged(object sender, EventArgs e)
  434.         {
  435.            if (tscbSize.SelectedItem == null)
  436.                 return;
  437.            FontFamily family = new FontFamily(tscbFont.SelectedItem.ToString());
  438.            float emSize = float.Parse(tscbSize.SelectedItem.ToString());
  439.            System.Drawing.Font newFont = new Font(family,emSize);;
  440.            rtMain.SelectionFont = newFont;
  441.            rtMain.Focus();          
  442.         }
  443.         private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
  444.         {
  445.             try
  446.             {
  447.                 frmFind f = new frmFind(this);
  448.                 f.Show();
  449.                
  450.             }
  451.             catch (Exception ex)
  452.             {
  453.                 MessageBox.Show(ex.Message.ToString(), "Error");
  454.             }
  455.         }
  456.         private void 替换SToolStripMenuItem_Click(object sender, EventArgs e)
  457.         {
  458.             try
  459.             {
  460.                 frmReplace f = new frmReplace(this);
  461.                 f.Show();
  462.                
  463.             }
  464.             catch (Exception ex)
  465.             {
  466.                 MessageBox.Show(ex.Message.ToString(), "Error");
  467.             }
  468.         }
  469.         private void 项目符号NToolStripMenuItem_Click(object sender, EventArgs e)
  470.         {
  471.             try
  472.             {
  473.                 项目符号NToolStripMenuItem.Checked = !项目符号NToolStripMenuItem.Checked;
  474.                 if (项目符号NToolStripMenuItem.Checked)
  475.                 {
  476.                     rtMain.BulletIndent = 10;
  477.                     rtMain.SelectionBullet = true;
  478.                 }
  479.                 else
  480.                     rtMain.SelectionBullet = false;
  481.             }
  482.             catch (Exception ex)
  483.             {
  484.                 MessageBox.Show(ex.Message.ToString(), "Error");
  485.             }
  486.         }
  487.         private void btnSend_Click(object sender, EventArgs e)
  488.         {
  489.             if (strEmail == "")
  490.             {
  491.                 MessageBox.Show("请先设置帐户信息!");
  492.                 return;
  493.             }
  494.             try
  495.             {
  496.                 //设置代理服务器
  497.                 //System.Net.WebProxy proxy = new System.Net.WebProxy();
  498.                 //proxy.UseDefaultCredentials = true;
  499.                 //proxy.BypassProxyOnLocal = true;
  500.                 //System.Net.GlobalProxySelection.Select = proxy;
  501.                 SmtpClient sc = new SmtpClient();
  502.                 sc.Host = strSMTP;
  503.                 sc.DeliveryMethod = SmtpDeliveryMethod.Network;
  504.                 MailMessage m = new MailMessage(strEmail, tbReceive.Text);
  505.                 if(tbCC.Text!="")
  506.                 {
  507.                     MailAddress copy = new MailAddress(tbCC.Text);
  508.                     m.CC.Add(copy);
  509.                 }
  510.                 m.Subject = tbSubject.Text;
  511.                 switch (cbPriority.SelectedIndex)
  512.                 {
  513.                     case 0:
  514.                         m.Priority = MailPriority.High;
  515.                         break;
  516.                     case 1:
  517.                         m.Priority = MailPriority.Normal;
  518.                         break;
  519.                     case 2:
  520.                         m.Priority = MailPriority.Low;
  521.                         break;
  522.                     default:
  523.                         break;
  524.                 }               
  525.                 if (cbFormat.SelectedIndex == 1)//text
  526.                 {
  527.                     m.IsBodyHtml = false;
  528.                     m.Body = rtMain.Text;
  529.                 }
  530.                 else//html
  531.                 {
  532.                     m.IsBodyHtml = true;
  533.                     m.Body = rtMain.GetHTML(true, true);
  534.                 }
  535.                 if (strFileName!="")
  536.                 {
  537.                     m.Attachments.Add(new Attachment(strFileName));
  538.                 }
  539.                 //以下设定身份验证
  540.                 NetworkCredential myCred = new NetworkCredential(strUserName, strPass);
  541.                 sc.Credentials = myCred;
  542.                 sc.Send(m);
  543.                 MessageBox.Show("发送成功!");
  544.             }
  545.             catch (SmtpException ex)
  546.             {
  547.                 MessageBox.Show(ex.Message);
  548.             }
  549.         }
  550.         private void 邮件账户设置ToolStripMenuItem_Click(object sender, EventArgs e)
  551.         {
  552.             frmSet dlg = new frmSet();
  553.             if (dlg.ShowDialog() == DialogResult.OK)
  554.             {
  555.                 strName = dlg.strName;
  556.                 strEmail = dlg.strEmail;
  557.                 strUserName = dlg.strUserName;
  558.                 strPass = dlg.strPass;
  559.                 strPOP3 = dlg.strPOP3;
  560.                 strSMTP = dlg.strSMTP;
  561.             }
  562.         }
  563.         private void tbInsertA_Click(object sender, EventArgs e)
  564.         {
  565.             OpenFileDialog openFileDialog = new OpenFileDialog();
  566.             openFileDialog.Filter = "所有文件|*.*";
  567.             openFileDialog.FilterIndex = 2;
  568.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  569.             {
  570.                 strFileName = openFileDialog.FileName;
  571.             }
  572.         }
  573.     }
  574. }