. NET 技术讨论

学于明志,交流增加见识,讨论改变思维
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.net写发送邮件工具

Posted on 2007-02-09 20:50    阅读(282)  评论(0编辑  收藏  举报
具体发送邮件的命名空间System.Net.Mail下有很多方法,下面是复制的代码;
具体的界面自己画一下就可以了,应该看的懂,具体代码如下:
 1private void button1_Click(object sender, EventArgs e)
 2
 3        {//发送
 4
 5            string strRecieveEMail = txtRecieveEMail.Text.Trim();
 6
 7            string strSendEmail = txtSendEMail.Text.Trim();
 8
 9            string strSubject = txtSubject.Text.Trim();
10
11            string strBody = txtBody.Text.Trim();
12
13 
14
15            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
16
17            msg.To.Add(strRecieveEMail);
18
19 
20
21            //System.Net.Mail.MailAddress mAdd = 
22
23 
24
25            if(txtAttachment.Text.Trim().Length > 0)
26
27            {
28
29                string FileName = txtAttachment.Text.Trim();
30
31                System.Net.Mail.Attachment pAttachment = new Attachment(FileName);
32
33                msg.Attachments.Add(pAttachment);
34
35            }

36
37            
38
39            msg.From = new MailAddress(strSendEmail, strSendEmail, System.Text.Encoding.UTF8);;
40
41            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
42
43            msg.Subject = strSubject;//邮件标题             
44
45            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
46
47            msg.Body = strBody;//邮件内容 
48
49            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
50
51            msg.IsBodyHtml = false;//是否是HTML邮件 
52
53            msg.Priority = MailPriority.High;//邮件优先级 
54
55 
56
57 
58
59            SmtpClient client = new SmtpClient();
60
61            client.Host = txtSmtp.Text.Trim();//这个非常重要,为邮件服务器设置
62
63 
64
65            object userState = msg;
66
67            try
68
69            {
70
71                client.SendAsync(msg, userState);    //简单一点儿可以client.Send(msg); 
72
73                MessageBox.Show("成功发送到" + strRecieveEMail + "邮箱");
74
75            }

76
77            catch (System.Net.Mail.SmtpException ex)
78
79            {
80
81                MessageBox.Show(ex.Message, "发送邮件出错");
82
83            }

84
85        }

86