Form1.cs
资源名称:yjal.rar [点击查看]
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:14k
源码类别:
Email客户端
开发平台:
Visual C++
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Net;
- using System.Net.Mail;
- namespace SendEmail
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.Label label5;
- private System.Windows.Forms.Label label6;
- private System.Windows.Forms.Label label7;
- private System.Windows.Forms.Button btn_Send;
- private System.Windows.Forms.TextBox txt_From;
- private System.Windows.Forms.TextBox txt_To;
- private System.Windows.Forms.TextBox txt_Subject;
- private System.Windows.Forms.TextBox txt_Body;
- private System.Windows.Forms.Button btn_Browse;
- private System.Windows.Forms.TextBox txt_Attachment;
- private System.Windows.Forms.TextBox txt_Cc;
- private System.Windows.Forms.Label label8;
- private System.Windows.Forms.ComboBox cmb_Priority;
- private System.Windows.Forms.OpenFileDialog openFileDialog1;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.label4 = new System.Windows.Forms.Label();
- this.label5 = new System.Windows.Forms.Label();
- this.label6 = new System.Windows.Forms.Label();
- this.txt_From = new System.Windows.Forms.TextBox();
- this.txt_To = new System.Windows.Forms.TextBox();
- this.txt_Cc = new System.Windows.Forms.TextBox();
- this.txt_Subject = new System.Windows.Forms.TextBox();
- this.txt_Body = new System.Windows.Forms.TextBox();
- this.btn_Browse = new System.Windows.Forms.Button();
- this.btn_Send = new System.Windows.Forms.Button();
- this.txt_Attachment = new System.Windows.Forms.TextBox();
- this.label7 = new System.Windows.Forms.Label();
- this.label8 = new System.Windows.Forms.Label();
- this.cmb_Priority = new System.Windows.Forms.ComboBox();
- this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
- this.SuspendLayout();
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(16, 48);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(53, 12);
- this.label1.TabIndex = 0;
- this.label1.Text = "发件人:";
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(16, 80);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(53, 12);
- this.label2.TabIndex = 1;
- this.label2.Text = "收件人:";
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.Location = new System.Drawing.Point(16, 112);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(53, 12);
- this.label3.TabIndex = 2;
- this.label3.Text = "抄 送:";
- //
- // label4
- //
- this.label4.AutoSize = true;
- this.label4.Location = new System.Drawing.Point(16, 176);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(53, 12);
- this.label4.TabIndex = 3;
- this.label4.Text = "附 件:";
- //
- // label5
- //
- this.label5.AutoSize = true;
- this.label5.Location = new System.Drawing.Point(16, 144);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(53, 12);
- this.label5.TabIndex = 4;
- this.label5.Text = "主 题:";
- //
- // label6
- //
- this.label6.AutoSize = true;
- this.label6.Location = new System.Drawing.Point(16, 240);
- this.label6.Name = "label6";
- this.label6.Size = new System.Drawing.Size(53, 12);
- this.label6.TabIndex = 5;
- this.label6.Text = "正 文:";
- //
- // txt_From
- //
- this.txt_From.Location = new System.Drawing.Point(80, 40);
- this.txt_From.Name = "txt_From";
- this.txt_From.Size = new System.Drawing.Size(440, 21);
- this.txt_From.TabIndex = 6;
- //
- // txt_To
- //
- this.txt_To.Location = new System.Drawing.Point(80, 72);
- this.txt_To.Name = "txt_To";
- this.txt_To.Size = new System.Drawing.Size(440, 21);
- this.txt_To.TabIndex = 7;
- //
- // txt_Cc
- //
- this.txt_Cc.Location = new System.Drawing.Point(80, 104);
- this.txt_Cc.Name = "txt_Cc";
- this.txt_Cc.Size = new System.Drawing.Size(440, 21);
- this.txt_Cc.TabIndex = 8;
- //
- // txt_Subject
- //
- this.txt_Subject.Location = new System.Drawing.Point(80, 136);
- this.txt_Subject.Name = "txt_Subject";
- this.txt_Subject.Size = new System.Drawing.Size(440, 21);
- this.txt_Subject.TabIndex = 9;
- //
- // txt_Body
- //
- this.txt_Body.Location = new System.Drawing.Point(16, 264);
- this.txt_Body.Multiline = true;
- this.txt_Body.Name = "txt_Body";
- this.txt_Body.Size = new System.Drawing.Size(504, 112);
- this.txt_Body.TabIndex = 10;
- //
- // btn_Browse
- //
- this.btn_Browse.Location = new System.Drawing.Point(456, 168);
- this.btn_Browse.Name = "btn_Browse";
- this.btn_Browse.Size = new System.Drawing.Size(64, 23);
- this.btn_Browse.TabIndex = 11;
- this.btn_Browse.Text = "浏览...";
- this.btn_Browse.Click += new System.EventHandler(this.btn_Browse_Click);
- //
- // btn_Send
- //
- this.btn_Send.Location = new System.Drawing.Point(448, 392);
- this.btn_Send.Name = "btn_Send";
- this.btn_Send.Size = new System.Drawing.Size(72, 24);
- this.btn_Send.TabIndex = 12;
- this.btn_Send.Text = "发 送";
- this.btn_Send.Click += new System.EventHandler(this.btn_Send_Click);
- //
- // txt_Attachment
- //
- this.txt_Attachment.Location = new System.Drawing.Point(80, 168);
- this.txt_Attachment.Name = "txt_Attachment";
- this.txt_Attachment.Size = new System.Drawing.Size(368, 21);
- this.txt_Attachment.TabIndex = 13;
- //
- // label7
- //
- this.label7.AutoSize = true;
- this.label7.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- this.label7.Location = new System.Drawing.Point(200, 8);
- this.label7.Name = "label7";
- this.label7.Size = new System.Drawing.Size(136, 21);
- this.label7.TabIndex = 14;
- this.label7.Text = "邮件发送系统";
- //
- // label8
- //
- this.label8.AutoSize = true;
- this.label8.Location = new System.Drawing.Point(16, 208);
- this.label8.Name = "label8";
- this.label8.Size = new System.Drawing.Size(53, 12);
- this.label8.TabIndex = 15;
- this.label8.Text = "优先级:";
- //
- // cmb_Priority
- //
- this.cmb_Priority.Location = new System.Drawing.Point(80, 206);
- this.cmb_Priority.Name = "cmb_Priority";
- this.cmb_Priority.Size = new System.Drawing.Size(80, 20);
- this.cmb_Priority.TabIndex = 16;
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(536, 430);
- this.Controls.Add(this.cmb_Priority);
- this.Controls.Add(this.label8);
- this.Controls.Add(this.label7);
- this.Controls.Add(this.txt_Attachment);
- this.Controls.Add(this.btn_Send);
- this.Controls.Add(this.btn_Browse);
- this.Controls.Add(this.txt_Body);
- this.Controls.Add(this.txt_Subject);
- this.Controls.Add(this.txt_Cc);
- this.Controls.Add(this.txt_To);
- this.Controls.Add(this.txt_From);
- this.Controls.Add(this.label6);
- this.Controls.Add(this.label5);
- this.Controls.Add(this.label4);
- this.Controls.Add(this.label3);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Name = "Form1";
- this.Text = "邮件发送系统";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void Form1_Load(object sender, System.EventArgs e)
- {
- //初始化优先级组合框和格式组合框
- cmb_Priority.Items.Add("高");
- cmb_Priority.Items.Add("低");
- cmb_Priority.Items.Add("普通");
- cmb_Priority.SelectedIndex = 2;
- }
- //发送
- private void btn_Send_Click(object sender, System.EventArgs e)
- {
- try
- {
- MailMessage myMail = new MailMessage(txt_From.Text.Trim(), txt_To.Text.Trim());
- if (txt_Cc.Text.Trim() != "")
- {
- myMail.CC.Add(txt_Cc.Text.Trim()); //设置抄送
- }
- myMail.Subject = txt_Subject.Text.Trim(); //设置邮件主题
- myMail.Body = txt_Body.Text.Trim(); //设置邮件正文
- //添加附件
- if (txt_Attachment.Text.Trim() != "")
- {
- char[] delim = new char[] { ',' };
- foreach (string att in txt_Attachment.Text.Trim().Split(delim))
- {
- Attachment myAttachment = new Attachment(att);
- myMail.Attachments.Add(myAttachment);
- }
- }
- //设置邮件优先级
- if (cmb_Priority.Text == "高")
- {
- myMail.Priority = MailPriority.High;
- }
- else if (cmb_Priority.Text == "低")
- {
- myMail.Priority = MailPriority.Low;
- }
- else
- {
- myMail.Priority = MailPriority.Normal;
- }
- //发送邮件
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "mail1.ufida.com.cn";
- smtp.UseDefaultCredentials = true;
- //smtp服务器需要验证
- smtp.Credentials = new NetworkCredential("hcq", "uf0000");
- smtp.Send(myMail);
- MessageBox.Show("邮件发送成功!");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- //添加附件
- private void btn_Browse_Click(object sender, System.EventArgs e)
- {
- openFileDialog1.Multiselect = true; //可以在打开文件对话框中同时选择多个文件
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- //将多个选定附件的名称显示在文本框中,并以逗号分隔
- foreach (string name in openFileDialog1.FileNames)
- {
- txt_Attachment.Text = txt_Attachment.Text + name + ",";
- }
- //去掉最后一个多余的逗号
- txt_Attachment.Text = txt_Attachment.Text.Substring(0, txt_Attachment.Text.Length - 1);
- }
- }
- //退出
- private void btn_Exit_Click(object sender, System.EventArgs e)
- {
- Application.Exit();
- }
- }
- }