www.pudn.com > SMTPMail.rar > Form1.cs


using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Web.Mail; 
 
namespace SMTPMail 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        //static void Main() 
        //{ 
        //    Application.Run(new Form1()); 
        //} 
 
        private void button3_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                MailMessage message = new MailMessage(); 
                if (this.textBox1.Text !="") 
                { 
                    message.To = this.textBox1.Text; 
                    message.BodyFormat = MailFormat.Text; 
                    if (this.radioButton2.Checked == true) 
                    { 
                        message.BodyFormat = MailFormat.Html; 
                    } 
                    message.BodyEncoding = System.Text.Encoding.UTF8; 
                    message.Subject = this.textBox3.Text; 
                    message.Body = this.richTextBox1.Text; 
 
                    if (this.textBox6.Text != "") 
                    { 
                        MailAttachment attach = new MailAttachment(this.textBox6.Text); 
                        message.Attachments.Add(attach); 
                    } 
 
                    if (this.textBox2.Text != "") 
                    { 
                        message.Cc = this.textBox2.Text; 
                    } 
 
                    message.From = this.textBox4.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; 
                    } 
 
 
 
                    ////利用163的SMTP来发送邮件 
                    //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
                    ////basic authentication 
                    //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", wrf529); 
                    //// 设置smtp服务器登录账号(如您163.com的信箱账号) 
                    //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", 859105295016); 
                    //// 设置smtp服务器登录密码 (如您的163.com的信箱账号密码) 
 
 
 
 
 
                    ////接着利用sina的SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本    
 
                    ////基本权限    
                    //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
                    ////用户名    
                    //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "wrf529"); 
                    ////密码    
                    //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "859105295016"); 
 
                    ////如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied    
 
 
                    //SmtpMail.SmtpServer = this.textBox5.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 button1_Click(object sender, EventArgs e) 
        { 
            OpenFileDialog oFileDialog = new OpenFileDialog(); 
            if (oFileDialog.ShowDialog() == DialogResult.OK) 
            { 
                this.textBox6.Text = oFileDialog.FileName; 
            } 
        } 
 
        private void radioButton1_CheckedChanged(object sender, EventArgs e) 
        { 
            if (this.radioButton1.Checked == true) 
            { 
                this.radioButton2.Checked = false; 
            } 
        } 
 
        private void radioButton2_CheckedChanged(object sender, EventArgs e) 
        { 
            if (this.radioButton2.Checked == true) 
            { 
                this.radioButton1.Checked = false; 
            } 
        } 
 
        private void button2_Click(object sender, EventArgs e) 
        { 
            this.textBox1.Text = ""; 
            this.textBox2.Text = ""; 
            this.textBox3.Text = ""; 
            this.textBox4.Text = ""; 
            this.textBox5.Text = ""; 
            this.textBox6.Text = ""; 
 
            this.richTextBox1.Text = ""; 
            this.comboBox1.SelectedIndex = 0; 
            this.radioButton1.Checked = true;           
        } 
 
    } 
}