VS2005下实现的一个简单邮件发送程序

今天上午自己写的一个非常简单的邮件发送程序,是用vs2005写的感觉跟vs2003有些不一样的东西,写的不好大家别骂。

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8using System.Net.Mail;
 9namespace MailSample_1
10{
11    public partial class Send : Form
12    {
13        public Send()
14        {
15            InitializeComponent();
16        }

17        private void SendMail()
18        {
19            try
20            {
21                MailAddress from = new MailAddress(txtFrom.Text);//发件人地址
22                MailAddress to = new MailAddress(txtTo.Text);//收件人地址
23                MailMessage Email = new MailMessage(from,to);                
24                Email.Subject = txtSubject.Text;//邮件标题
25                if(txtAttachment.Text!=null&&!txtAttachment.Text.Equals(""))
26                {
27                    Attachment att = new Attachment(txtAttachment.Text);//实例化一个附件对象
28                    Email.Attachments.Add(att);//向邮件添加附件
29                }

30                Email.Body = txtMessage.Text;//邮件正文
31                Email.Priority = MailPriority.Normal;//邮件的优先级别
32                Email.BodyEncoding = Encoding.Unicode;//邮件的编码方式
33                Email.IsBodyHtml = true;//是否按html格式发送
34                SmtpClient client = new SmtpClient("smtp.ziton.com.cn");//设置发件服务器                
35                client.Credentials = new System.Net.NetworkCredential("用户名","密码");//邮箱的用户名和密码
36                client.DeliveryMethod = SmtpDeliveryMethod.Network;             
37                client.Send(Email);
38                MessageBox.Show("邮件已发出");
39            }

40            catch (Exception ex)
41            {                
42               MessageBox.Show(ex.ToString());
43            }
            
44        }

45
46        private void button1_Click(object sender, EventArgs e)
47        {
48            this.SendMail();
49        }

50
51        private void 发件服务器配置ToolStripMenuItem_Click(object sender, EventArgs e)
52        {
53            MessageBox.Show("正在建设中。。。。");
54        }

55
56        private void butAttachment_Click(object sender, EventArgs e)
57        {
58            DialogResult dr = AttachmentDialog.ShowDialog();
59            if (dr.Equals(DialogResult.OK))
60            {
61                txtAttachment.Text = AttachmentDialog.FileName;
62            }

63        }

64
65        private void Send_Load(object sender, EventArgs e)
66        {
67            txtFrom.Focus();
68        }

69    }

70}

71
posted on 2007-05-29 14:52  人是逼出来的  阅读(290)  评论(0)    收藏  举报