C#2005和2008发送邮件的测试例子

C#2005和2008发送邮件的测试例子,书上和帮助里,缺少用户验证部分

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
//using System.IO;
//using System.Net;


namespace EX15_06
{
    public partial class Form1 : Form
    {
      
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
        MailMessage Mail;
        SmtpClient Client;
        public  void CreateTimeoutTestMessage(string server)
        {
            try
            {
                //实例MailMessage类
                Mail = new MailMessage(this.txtFrom.Text, this.txtGet.Text);
                Mail.Subject =this.txtSubject.Text.Trim().ToString();
                Mail.Body = this.richTextBox1.Text.Trim().ToString();
                //Mail.c
                Client = new SmtpClient(server, 25);//实例一个SmtpClient类

 

                //此行的内容,书上和帮助里缺少,需增加

                Client.Credentials = new System.Net.NetworkCredential("用户邮件地址 ", "用户密码");//用户名和密码

 

                Client.Send(Mail);
                MessageBox.Show("邮件发送成功!!!");
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.richTextBox1.Text.Trim().ToString() != "")
            {
                CreateTimeoutTestMessage("smtp.126.com");//参数传的是smtp服务器的名称
            }
            else
            {
                MessageBox.Show("请认真填写邮件!");
                return;
            }
        }
    }
}

 

 

namespace EX15_06
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.txtFrom = new System.Windows.Forms.TextBox();
            this.txtGet = new System.Windows.Forms.TextBox();
            this.txtSubject = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(286, 59);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "发送";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // txtFrom
            //
            this.txtFrom.Location = new System.Drawing.Point(67, 4);
            this.txtFrom.Name = "txtFrom";
            this.txtFrom.Size = new System.Drawing.Size(294, 21);
            this.txtFrom.TabIndex = 1;
            this.txtFrom.Text = "发送邮件@126.com ";
            //
            // txtGet
            //
            this.txtGet.Location = new System.Drawing.Point(67, 32);
            this.txtGet.Name = "txtGet";
            this.txtGet.Size = new System.Drawing.Size(294, 21);
            this.txtGet.TabIndex = 2;
            this.txtGet.Text = "接收邮件@126.com ";
            //
            // txtSubject
            //
            this.txtSubject.Location = new System.Drawing.Point(67, 59);
            this.txtSubject.Name = "txtSubject";
            this.txtSubject.Size = new System.Drawing.Size(215, 21);
            this.txtSubject.TabIndex = 3;
            this.txtSubject.Text = "test";
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(8, 35);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 12);
            this.label1.TabIndex = 4;
            this.label1.Text = "收件人:";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(8, 7);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 5;
            this.label2.Text = "发件人:";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(20, 62);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 6;
            this.label3.Text = "主题:";
            //
            // richTextBox1
            //
            this.richTextBox1.Location = new System.Drawing.Point(4, 86);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(357, 155);
            this.richTextBox1.TabIndex = 7;
            this.richTextBox1.Text = "test1";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(364, 247);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtSubject);
            this.Controls.Add(this.txtGet);
            this.Controls.Add(this.txtFrom);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "发送电子邮件";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox txtFrom;
        private System.Windows.Forms.TextBox txtGet;
        private System.Windows.Forms.TextBox txtSubject;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.RichTextBox richTextBox1;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值