Form1.cs
上传用户:youdujia
上传日期:2017-09-03
资源大小:100k
文件大小:15k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Web.Mail;
- namespace MailSend
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.TextBox txbRecPer;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.TextBox txbCopy;
- private System.Windows.Forms.TextBox txbSub;
- private System.Windows.Forms.TextBox txbSender;
- private System.Windows.Forms.Label label5;
- private System.Windows.Forms.TextBox txbServer;
- private System.Windows.Forms.GroupBox groupBox4;
- private System.Windows.Forms.Button btnSend;
- private System.Windows.Forms.RichTextBox rtbMail;
- private System.Windows.Forms.GroupBox groupBox3;
- private System.Windows.Forms.Label label6;
- private System.Windows.Forms.TextBox txbAdd;
- private System.Windows.Forms.Button btnAdd;
- private System.Windows.Forms.ComboBox comboBox1;
- private System.Windows.Forms.Label label7;
- //
- private System.Windows.Forms.Label label8;
- private System.Windows.Forms.Label label9;
- private System.Windows.Forms.TextBox txbPassword;
- private System.Windows.Forms.TextBox txbUserName;
- // /// <summary>
- // /// 邮件服务器端口号
- // /// </summary>
- // private int mailserverport=25;
- //
- // /// SMTP认证时使用的用户名
- // /// </summary>
- // private string username="";
- // /// SMTP认证时使用的密码
- // private string password="";
- /// <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.groupBox1 = new System.Windows.Forms.GroupBox();
- this.txbUserName = new System.Windows.Forms.TextBox();
- this.txbPassword = new System.Windows.Forms.TextBox();
- this.label9 = new System.Windows.Forms.Label();
- this.label8 = new System.Windows.Forms.Label();
- this.txbSender = new System.Windows.Forms.TextBox();
- this.txbSub = new System.Windows.Forms.TextBox();
- this.txbCopy = new System.Windows.Forms.TextBox();
- this.label4 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.txbRecPer = new System.Windows.Forms.TextBox();
- this.label1 = new System.Windows.Forms.Label();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.txbServer = new System.Windows.Forms.TextBox();
- this.label5 = new System.Windows.Forms.Label();
- this.btnSend = new System.Windows.Forms.Button();
- this.groupBox4 = new System.Windows.Forms.GroupBox();
- this.rtbMail = new System.Windows.Forms.RichTextBox();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.label7 = new System.Windows.Forms.Label();
- this.comboBox1 = new System.Windows.Forms.ComboBox();
- this.btnAdd = new System.Windows.Forms.Button();
- this.txbAdd = new System.Windows.Forms.TextBox();
- this.label6 = new System.Windows.Forms.Label();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.groupBox4.SuspendLayout();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.txbUserName);
- this.groupBox1.Controls.Add(this.txbPassword);
- this.groupBox1.Controls.Add(this.label9);
- this.groupBox1.Controls.Add(this.label8);
- this.groupBox1.Controls.Add(this.txbSender);
- this.groupBox1.Controls.Add(this.txbSub);
- this.groupBox1.Controls.Add(this.txbCopy);
- this.groupBox1.Controls.Add(this.label4);
- this.groupBox1.Controls.Add(this.label3);
- this.groupBox1.Controls.Add(this.label2);
- this.groupBox1.Controls.Add(this.txbRecPer);
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Location = new System.Drawing.Point(8, 8);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(232, 248);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "邮件:";
- //
- // txbUserName
- //
- this.txbUserName.Location = new System.Drawing.Point(64, 64);
- this.txbUserName.Name = "txbUserName";
- this.txbUserName.Size = new System.Drawing.Size(160, 21);
- this.txbUserName.TabIndex = 11;
- this.txbUserName.Text = "hrk11111";
- //
- // txbPassword
- //
- this.txbPassword.Location = new System.Drawing.Point(64, 96);
- this.txbPassword.Name = "txbPassword";
- this.txbPassword.PasswordChar = '*';
- this.txbPassword.Size = new System.Drawing.Size(160, 21);
- this.txbPassword.TabIndex = 10;
- this.txbPassword.Text = "19831022";
- //
- // label9
- //
- this.label9.Location = new System.Drawing.Point(16, 64);
- this.label9.Name = "label9";
- this.label9.Size = new System.Drawing.Size(48, 23);
- this.label9.TabIndex = 9;
- this.label9.Text = "用户名:";
- //
- // label8
- //
- this.label8.Location = new System.Drawing.Point(16, 96);
- this.label8.Name = "label8";
- this.label8.Size = new System.Drawing.Size(48, 23);
- this.label8.TabIndex = 8;
- this.label8.Text = "密 码:";
- //
- // txbSender
- //
- this.txbSender.Location = new System.Drawing.Point(64, 208);
- this.txbSender.Name = "txbSender";
- this.txbSender.Size = new System.Drawing.Size(160, 21);
- this.txbSender.TabIndex = 7;
- this.txbSender.Text = "hrk11111@126.com";
- //
- // txbSub
- //
- this.txbSub.Location = new System.Drawing.Point(64, 168);
- this.txbSub.Name = "txbSub";
- this.txbSub.Size = new System.Drawing.Size(160, 21);
- this.txbSub.TabIndex = 6;
- this.txbSub.Text = "发送邮件";
- //
- // txbCopy
- //
- this.txbCopy.Location = new System.Drawing.Point(64, 128);
- this.txbCopy.Name = "txbCopy";
- this.txbCopy.Size = new System.Drawing.Size(160, 21);
- this.txbCopy.TabIndex = 5;
- this.txbCopy.Text = "";
- //
- // label4
- //
- this.label4.Location = new System.Drawing.Point(16, 208);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(48, 23);
- this.label4.TabIndex = 4;
- this.label4.Text = "发件人:";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(16, 168);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(48, 23);
- this.label3.TabIndex = 3;
- this.label3.Text = "主 题:";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(16, 128);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(48, 23);
- this.label2.TabIndex = 2;
- this.label2.Text = "抄 送:";
- //
- // txbRecPer
- //
- this.txbRecPer.Location = new System.Drawing.Point(64, 32);
- this.txbRecPer.Name = "txbRecPer";
- this.txbRecPer.Size = new System.Drawing.Size(160, 21);
- this.txbRecPer.TabIndex = 1;
- this.txbRecPer.Text = "hrk_1983@126.com";
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(16, 32);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(48, 23);
- this.label1.TabIndex = 0;
- this.label1.Text = "收件人:";
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.txbServer);
- this.groupBox2.Controls.Add(this.label5);
- this.groupBox2.Controls.Add(this.btnSend);
- this.groupBox2.Location = new System.Drawing.Point(248, 8);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(208, 248);
- this.groupBox2.TabIndex = 1;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "SMTP服务器:";
- //
- // txbServer
- //
- this.txbServer.Location = new System.Drawing.Point(8, 64);
- this.txbServer.Name = "txbServer";
- this.txbServer.Size = new System.Drawing.Size(144, 21);
- this.txbServer.TabIndex = 1;
- this.txbServer.Text = "smtp.126.com";
- //
- // label5
- //
- this.label5.Location = new System.Drawing.Point(8, 32);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(104, 23);
- this.label5.TabIndex = 0;
- this.label5.Text = "邮件发送服务器:";
- //
- // btnSend
- //
- this.btnSend.Location = new System.Drawing.Point(96, 168);
- this.btnSend.Name = "btnSend";
- this.btnSend.Size = new System.Drawing.Size(96, 23);
- this.btnSend.TabIndex = 4;
- this.btnSend.Text = "发送邮件";
- this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
- //
- // groupBox4
- //
- this.groupBox4.Controls.Add(this.rtbMail);
- this.groupBox4.Location = new System.Drawing.Point(8, 368);
- this.groupBox4.Name = "groupBox4";
- this.groupBox4.Size = new System.Drawing.Size(448, 184);
- this.groupBox4.TabIndex = 3;
- this.groupBox4.TabStop = false;
- this.groupBox4.Text = "邮件内容:";
- //
- // rtbMail
- //
- this.rtbMail.Location = new System.Drawing.Point(0, 24);
- this.rtbMail.Name = "rtbMail";
- this.rtbMail.Size = new System.Drawing.Size(488, 160);
- this.rtbMail.TabIndex = 0;
- this.rtbMail.Text = "";
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.label7);
- this.groupBox3.Controls.Add(this.comboBox1);
- this.groupBox3.Controls.Add(this.btnAdd);
- this.groupBox3.Controls.Add(this.txbAdd);
- this.groupBox3.Controls.Add(this.label6);
- this.groupBox3.Location = new System.Drawing.Point(8, 264);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(448, 96);
- this.groupBox3.TabIndex = 4;
- this.groupBox3.TabStop = false;
- this.groupBox3.Text = "附件传送:";
- //
- // label7
- //
- this.label7.Location = new System.Drawing.Point(16, 64);
- this.label7.Name = "label7";
- this.label7.Size = new System.Drawing.Size(64, 23);
- this.label7.TabIndex = 4;
- this.label7.Text = "优先级别:";
- //
- // comboBox1
- //
- this.comboBox1.Items.AddRange(new object[] {
- "正常",
- "高",
- "低"});
- this.comboBox1.Location = new System.Drawing.Point(80, 64);
- this.comboBox1.Name = "comboBox1";
- this.comboBox1.Size = new System.Drawing.Size(56, 20);
- this.comboBox1.TabIndex = 3;
- this.comboBox1.Text = "正常";
- //
- // btnAdd
- //
- this.btnAdd.Location = new System.Drawing.Point(248, 24);
- this.btnAdd.Name = "btnAdd";
- this.btnAdd.TabIndex = 2;
- this.btnAdd.Text = "添加";
- this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
- //
- // txbAdd
- //
- this.txbAdd.Location = new System.Drawing.Point(64, 24);
- this.txbAdd.Name = "txbAdd";
- this.txbAdd.Size = new System.Drawing.Size(160, 21);
- this.txbAdd.TabIndex = 1;
- this.txbAdd.Text = "";
- //
- // label6
- //
- this.label6.Location = new System.Drawing.Point(16, 24);
- this.label6.Name = "label6";
- this.label6.Size = new System.Drawing.Size(48, 23);
- this.label6.TabIndex = 0;
- this.label6.Text = "附 件:";
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.AutoScroll = true;
- this.ClientSize = new System.Drawing.Size(464, 558);
- this.Controls.Add(this.groupBox3);
- this.Controls.Add(this.groupBox4);
- this.Controls.Add(this.groupBox1);
- this.Controls.Add(this.groupBox2);
- this.HelpButton = true;
- this.Name = "Form1";
- this.Text = "发送邮件";
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.groupBox4.ResumeLayout(false);
- this.groupBox3.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void btnSend_Click(object sender, System.EventArgs e)
- {
- try
- {
- MailMessage Message = new MailMessage();
- //设置为需要用户验证
- Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
- //设置smtpserver
- Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver",this.txbServer.Text);
- //设置验证用户名
- Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",this.txbUserName.Text);
- //设置验证密码
- Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",this.txbPassword.Text);
- if(this.txbRecPer.Text != "")
- {
- //收件人
- Message.To = this.txbRecPer.Text;
- //正文类型
- Message.BodyFormat = MailFormat.Text;
- //编码类型
- Message.BodyEncoding = System.Text.Encoding.UTF8;
- //主题
- Message.Subject = this.txbSub.Text;
- //正文
- Message.Body = this.rtbMail.Text + "<End>";
- if(this.txbAdd.Text != "")
- {
- //构造一个附件对象
- MailAttachment add = new MailAttachment(this.txbAdd.Text);
- //添加附件
- Message.Attachments.Add(add);
- }
- //判断是否有抄送
- if(this.txbCopy.Text != "")
- {
- Message.Cc = this.txbCopy.Text;
- }
- //发件人
- Message.From = this.txbSender.Text;
- //设置优先等级
- int i = this.comboBox1.SelectedIndex;
- if(i == 0)
- {
- Message.Priority = MailPriority.Normal;
- }
- if(i == 1)
- {
- Message.Priority = MailPriority.High;
- }
- if(i == 2)
- {
- Message.Priority = MailPriority.Low;
- }
- //设置邮件发送服务器
- SmtpMail.SmtpServer = this.txbServer.Text;
- //发送邮件
- SmtpMail.Send(Message);
- MessageBox.Show("邮件发送已经成功!","恭喜"
- ,MessageBoxButtons.OK,MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show("未指定收件人地址","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
- }
- }
- catch(Exception error)
- {
- MessageBox.Show("邮件发送失败信息:"+error.Message,"失败",
- MessageBoxButtons.OK,MessageBoxIcon.Information);
-
- }
- }
- private void btnAdd_Click(object sender, System.EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- if(openFileDialog.ShowDialog() == DialogResult.OK)
- {
- this.txbAdd.Text = openFileDialog.FileName;
- }
- }
- }
- }