Form1.cs
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:14k
源码类别:

Email客户端

开发平台:

Visual C++

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net;
  8. using System.Net.Mail;
  9. namespace SendEmail
  10. {
  11.     /// <summary>
  12.     /// Form1 的摘要说明。
  13.     /// </summary>
  14.     public class Form1 : System.Windows.Forms.Form
  15.     {
  16.         private System.Windows.Forms.Label label1;
  17.         private System.Windows.Forms.Label label2;
  18.         private System.Windows.Forms.Label label3;
  19.         private System.Windows.Forms.Label label4;
  20.         private System.Windows.Forms.Label label5;
  21.         private System.Windows.Forms.Label label6;
  22.         private System.Windows.Forms.Label label7;
  23.         private System.Windows.Forms.Button btn_Send;
  24.         private System.Windows.Forms.TextBox txt_From;
  25.         private System.Windows.Forms.TextBox txt_To;
  26.         private System.Windows.Forms.TextBox txt_Subject;
  27.         private System.Windows.Forms.TextBox txt_Body;
  28.         private System.Windows.Forms.Button btn_Browse;
  29.         private System.Windows.Forms.TextBox txt_Attachment;
  30.         private System.Windows.Forms.TextBox txt_Cc;
  31.         private System.Windows.Forms.Label label8;
  32.         private System.Windows.Forms.ComboBox cmb_Priority;
  33.         private System.Windows.Forms.OpenFileDialog openFileDialog1;
  34.         /// <summary>
  35.         /// 必需的设计器变量。
  36.         /// </summary>
  37.         private System.ComponentModel.Container components = null;
  38.         public Form1()
  39.         {
  40.             //
  41.             // Windows 窗体设计器支持所必需的
  42.             //
  43.             InitializeComponent();
  44.             //
  45.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  46.             //
  47.         }
  48.         /// <summary>
  49.         /// 清理所有正在使用的资源。
  50.         /// </summary>
  51.         protected override void Dispose(bool disposing)
  52.         {
  53.             if (disposing)
  54.             {
  55.                 if (components != null)
  56.                 {
  57.                     components.Dispose();
  58.                 }
  59.             }
  60.             base.Dispose(disposing);
  61.         }
  62.         #region Windows 窗体设计器生成的代码
  63.         /// <summary>
  64.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  65.         /// 此方法的内容。
  66.         /// </summary>
  67.         private void InitializeComponent()
  68.         {
  69.             this.label1 = new System.Windows.Forms.Label();
  70.             this.label2 = new System.Windows.Forms.Label();
  71.             this.label3 = new System.Windows.Forms.Label();
  72.             this.label4 = new System.Windows.Forms.Label();
  73.             this.label5 = new System.Windows.Forms.Label();
  74.             this.label6 = new System.Windows.Forms.Label();
  75.             this.txt_From = new System.Windows.Forms.TextBox();
  76.             this.txt_To = new System.Windows.Forms.TextBox();
  77.             this.txt_Cc = new System.Windows.Forms.TextBox();
  78.             this.txt_Subject = new System.Windows.Forms.TextBox();
  79.             this.txt_Body = new System.Windows.Forms.TextBox();
  80.             this.btn_Browse = new System.Windows.Forms.Button();
  81.             this.btn_Send = new System.Windows.Forms.Button();
  82.             this.txt_Attachment = new System.Windows.Forms.TextBox();
  83.             this.label7 = new System.Windows.Forms.Label();
  84.             this.label8 = new System.Windows.Forms.Label();
  85.             this.cmb_Priority = new System.Windows.Forms.ComboBox();
  86.             this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  87.             this.SuspendLayout();
  88.             // 
  89.             // label1
  90.             // 
  91.             this.label1.AutoSize = true;
  92.             this.label1.Location = new System.Drawing.Point(16, 48);
  93.             this.label1.Name = "label1";
  94.             this.label1.Size = new System.Drawing.Size(53, 12);
  95.             this.label1.TabIndex = 0;
  96.             this.label1.Text = "发件人:";
  97.             // 
  98.             // label2
  99.             // 
  100.             this.label2.AutoSize = true;
  101.             this.label2.Location = new System.Drawing.Point(16, 80);
  102.             this.label2.Name = "label2";
  103.             this.label2.Size = new System.Drawing.Size(53, 12);
  104.             this.label2.TabIndex = 1;
  105.             this.label2.Text = "收件人:";
  106.             // 
  107.             // label3
  108.             // 
  109.             this.label3.AutoSize = true;
  110.             this.label3.Location = new System.Drawing.Point(16, 112);
  111.             this.label3.Name = "label3";
  112.             this.label3.Size = new System.Drawing.Size(53, 12);
  113.             this.label3.TabIndex = 2;
  114.             this.label3.Text = "抄  送:";
  115.             // 
  116.             // label4
  117.             // 
  118.             this.label4.AutoSize = true;
  119.             this.label4.Location = new System.Drawing.Point(16, 176);
  120.             this.label4.Name = "label4";
  121.             this.label4.Size = new System.Drawing.Size(53, 12);
  122.             this.label4.TabIndex = 3;
  123.             this.label4.Text = "附  件:";
  124.             // 
  125.             // label5
  126.             // 
  127.             this.label5.AutoSize = true;
  128.             this.label5.Location = new System.Drawing.Point(16, 144);
  129.             this.label5.Name = "label5";
  130.             this.label5.Size = new System.Drawing.Size(53, 12);
  131.             this.label5.TabIndex = 4;
  132.             this.label5.Text = "主  题:";
  133.             // 
  134.             // label6
  135.             // 
  136.             this.label6.AutoSize = true;
  137.             this.label6.Location = new System.Drawing.Point(16, 240);
  138.             this.label6.Name = "label6";
  139.             this.label6.Size = new System.Drawing.Size(53, 12);
  140.             this.label6.TabIndex = 5;
  141.             this.label6.Text = "正  文:";
  142.             // 
  143.             // txt_From
  144.             // 
  145.             this.txt_From.Location = new System.Drawing.Point(80, 40);
  146.             this.txt_From.Name = "txt_From";
  147.             this.txt_From.Size = new System.Drawing.Size(440, 21);
  148.             this.txt_From.TabIndex = 6;
  149.             // 
  150.             // txt_To
  151.             // 
  152.             this.txt_To.Location = new System.Drawing.Point(80, 72);
  153.             this.txt_To.Name = "txt_To";
  154.             this.txt_To.Size = new System.Drawing.Size(440, 21);
  155.             this.txt_To.TabIndex = 7;
  156.             // 
  157.             // txt_Cc
  158.             // 
  159.             this.txt_Cc.Location = new System.Drawing.Point(80, 104);
  160.             this.txt_Cc.Name = "txt_Cc";
  161.             this.txt_Cc.Size = new System.Drawing.Size(440, 21);
  162.             this.txt_Cc.TabIndex = 8;
  163.             // 
  164.             // txt_Subject
  165.             // 
  166.             this.txt_Subject.Location = new System.Drawing.Point(80, 136);
  167.             this.txt_Subject.Name = "txt_Subject";
  168.             this.txt_Subject.Size = new System.Drawing.Size(440, 21);
  169.             this.txt_Subject.TabIndex = 9;
  170.             // 
  171.             // txt_Body
  172.             // 
  173.             this.txt_Body.Location = new System.Drawing.Point(16, 264);
  174.             this.txt_Body.Multiline = true;
  175.             this.txt_Body.Name = "txt_Body";
  176.             this.txt_Body.Size = new System.Drawing.Size(504, 112);
  177.             this.txt_Body.TabIndex = 10;
  178.             // 
  179.             // btn_Browse
  180.             // 
  181.             this.btn_Browse.Location = new System.Drawing.Point(456, 168);
  182.             this.btn_Browse.Name = "btn_Browse";
  183.             this.btn_Browse.Size = new System.Drawing.Size(64, 23);
  184.             this.btn_Browse.TabIndex = 11;
  185.             this.btn_Browse.Text = "浏览...";
  186.             this.btn_Browse.Click += new System.EventHandler(this.btn_Browse_Click);
  187.             // 
  188.             // btn_Send
  189.             // 
  190.             this.btn_Send.Location = new System.Drawing.Point(448, 392);
  191.             this.btn_Send.Name = "btn_Send";
  192.             this.btn_Send.Size = new System.Drawing.Size(72, 24);
  193.             this.btn_Send.TabIndex = 12;
  194.             this.btn_Send.Text = "发  送";
  195.             this.btn_Send.Click += new System.EventHandler(this.btn_Send_Click);
  196.             // 
  197.             // txt_Attachment
  198.             // 
  199.             this.txt_Attachment.Location = new System.Drawing.Point(80, 168);
  200.             this.txt_Attachment.Name = "txt_Attachment";
  201.             this.txt_Attachment.Size = new System.Drawing.Size(368, 21);
  202.             this.txt_Attachment.TabIndex = 13;
  203.             // 
  204.             // label7
  205.             // 
  206.             this.label7.AutoSize = true;
  207.             this.label7.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  208.             this.label7.Location = new System.Drawing.Point(200, 8);
  209.             this.label7.Name = "label7";
  210.             this.label7.Size = new System.Drawing.Size(136, 21);
  211.             this.label7.TabIndex = 14;
  212.             this.label7.Text = "邮件发送系统";
  213.             // 
  214.             // label8
  215.             // 
  216.             this.label8.AutoSize = true;
  217.             this.label8.Location = new System.Drawing.Point(16, 208);
  218.             this.label8.Name = "label8";
  219.             this.label8.Size = new System.Drawing.Size(53, 12);
  220.             this.label8.TabIndex = 15;
  221.             this.label8.Text = "优先级:";
  222.             // 
  223.             // cmb_Priority
  224.             // 
  225.             this.cmb_Priority.Location = new System.Drawing.Point(80, 206);
  226.             this.cmb_Priority.Name = "cmb_Priority";
  227.             this.cmb_Priority.Size = new System.Drawing.Size(80, 20);
  228.             this.cmb_Priority.TabIndex = 16;
  229.             // 
  230.             // Form1
  231.             // 
  232.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  233.             this.ClientSize = new System.Drawing.Size(536, 430);
  234.             this.Controls.Add(this.cmb_Priority);
  235.             this.Controls.Add(this.label8);
  236.             this.Controls.Add(this.label7);
  237.             this.Controls.Add(this.txt_Attachment);
  238.             this.Controls.Add(this.btn_Send);
  239.             this.Controls.Add(this.btn_Browse);
  240.             this.Controls.Add(this.txt_Body);
  241.             this.Controls.Add(this.txt_Subject);
  242.             this.Controls.Add(this.txt_Cc);
  243.             this.Controls.Add(this.txt_To);
  244.             this.Controls.Add(this.txt_From);
  245.             this.Controls.Add(this.label6);
  246.             this.Controls.Add(this.label5);
  247.             this.Controls.Add(this.label4);
  248.             this.Controls.Add(this.label3);
  249.             this.Controls.Add(this.label2);
  250.             this.Controls.Add(this.label1);
  251.             this.Name = "Form1";
  252.             this.Text = "邮件发送系统";
  253.             this.Load += new System.EventHandler(this.Form1_Load);
  254.             this.ResumeLayout(false);
  255.             this.PerformLayout();
  256.         }
  257.         #endregion
  258.         /// <summary>
  259.         /// 应用程序的主入口点。
  260.         /// </summary>
  261.         [STAThread]
  262.         static void Main()
  263.         {
  264.             Application.Run(new Form1());
  265.         }
  266.         private void Form1_Load(object sender, System.EventArgs e)
  267.         {
  268.             //初始化优先级组合框和格式组合框
  269.             cmb_Priority.Items.Add("高");
  270.             cmb_Priority.Items.Add("低");
  271.             cmb_Priority.Items.Add("普通");
  272.             cmb_Priority.SelectedIndex = 2;
  273.         }
  274.         //发送
  275.         private void btn_Send_Click(object sender, System.EventArgs e)
  276.         {
  277.             try
  278.             {
  279.                 MailMessage myMail = new MailMessage(txt_From.Text.Trim(), txt_To.Text.Trim());
  280.                 if (txt_Cc.Text.Trim() != "")
  281.                 {
  282.                     myMail.CC.Add(txt_Cc.Text.Trim());              //设置抄送
  283.                 }
  284.                 myMail.Subject = txt_Subject.Text.Trim();   //设置邮件主题
  285.                 myMail.Body = txt_Body.Text.Trim();         //设置邮件正文
  286.                 //添加附件
  287.                 if (txt_Attachment.Text.Trim() != "")
  288.                 {
  289.                     char[] delim = new char[] { ',' };
  290.                     foreach (string att in txt_Attachment.Text.Trim().Split(delim))
  291.                     {
  292.                         Attachment myAttachment = new Attachment(att);
  293.                         myMail.Attachments.Add(myAttachment);
  294.                     }
  295.                 }
  296.                 //设置邮件优先级
  297.                 if (cmb_Priority.Text == "高")
  298.                 {
  299.                     myMail.Priority = MailPriority.High;
  300.                 }
  301.                 else if (cmb_Priority.Text == "低")
  302.                 {
  303.                     myMail.Priority = MailPriority.Low;
  304.                 }
  305.                 else
  306.                 {
  307.                     myMail.Priority = MailPriority.Normal;
  308.                 }
  309.                 //发送邮件
  310.                 SmtpClient smtp = new SmtpClient();
  311.                 smtp.Host = "mail1.ufida.com.cn";
  312.                 smtp.UseDefaultCredentials = true;
  313.                 //smtp服务器需要验证
  314.                 smtp.Credentials = new NetworkCredential("hcq", "uf0000");
  315.                 smtp.Send(myMail);
  316.                 MessageBox.Show("邮件发送成功!");
  317.             }
  318.             catch (Exception ex)
  319.             {
  320.                 MessageBox.Show(ex.Message);
  321.             }
  322.         }
  323.         //添加附件
  324.         private void btn_Browse_Click(object sender, System.EventArgs e)
  325.         {
  326.             openFileDialog1.Multiselect = true;   //可以在打开文件对话框中同时选择多个文件
  327.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  328.             {
  329.                 //将多个选定附件的名称显示在文本框中,并以逗号分隔
  330.                 foreach (string name in openFileDialog1.FileNames)
  331.                 {
  332.                     txt_Attachment.Text = txt_Attachment.Text + name + ",";
  333.                 }
  334.                 //去掉最后一个多余的逗号
  335.                 txt_Attachment.Text = txt_Attachment.Text.Substring(0, txt_Attachment.Text.Length - 1);
  336.             }
  337.         }
  338.         //退出
  339.         private void btn_Exit_Click(object sender, System.EventArgs e)
  340.         {
  341.             Application.Exit();
  342.         }
  343.     }
  344. }