用asp.net发送电子邮件

    .net提供了强大的功能,其中就包括发送邮件上.经过仔细研究才发现用.net发送电子邮件是很简单的一件事情.现在我们就来看一下.
    首先添加命名空间
None.gifUsing System.Net.Mail
下面我们看发送邮件的代码部分:( 注意:网易的邮件服务器是smtp.163.com)
None.gifMailAddress from  =   new  MailAddress(TBMailFrom.Text);
None.gif        MailAddress to 
=   new  MailAddress(TBMailTo.Text);
None.gif        MailMessage message 
=   new  MailMessage(from,to);
None.gif        message.Subject 
=  TBSubject.Text;
None.gif        message.Body 
=  TBDescript.Text;
None.gif        
if  (FileUpload1.PostedFile.FileName  !=   "" )
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Attachment att 
= new Attachment(FileUpload1.PostedFile.FileName);
InBlock.gif            message.Attachments.Add(att);
ExpandedBlockEnd.gif        }

None.gif        SmtpClient client 
=   new  SmtpClient( " smtp.163.com " );
None.gifsmtp.Credentials 
=   new  System.Net.NetworkCredential( " username " " password " );
None.gif        client.Send(message);

我们这里在发送邮件的时候只是设置了邮件收,发件人,邮件主题和邮件正文部门,在.net里面还可以设置发送邮件的文本格式,优先级等.我们这里就不说明了,相信看看就会明白的.(message.Priority;设置优先级.mssage.Headers;设置邮件的标头. message.CC;设置抄送.message.IsBodyHtml;设置是否以html格式发送邮件)
下面是对页面的设置:
 1None.gif< body >
 2 None.gif     < form id = " form1 "  runat = " server " >
 3 None.gif     < div >
 4 None.gif         < table style = " width: 268px " >
 5 None.gif         < tr >< td >
 6 None.gif             < asp:Label ID = " Label4 "  runat = " server "  Text = " 发件人: " ></ asp:Label ></ td >
 7 None.gif             < td >
 8 None.gif                 < asp:TextBox ID = " TBMailFrom "  runat = " server " ></ asp:TextBox ></ td ></ tr >
 9 None.gif             < tr >
10 None.gif                 < td style = " width: 101px " >
11 None.gif                     < asp:Label ID = " Label1 "  runat = " server "  Text = "  收件人: " ></ asp:Label ></ td >
12 None.gif                 < td >
13 None.gif                     < asp:TextBox ID = " TBMailTo "  runat = " server " ></ asp:TextBox ></ td >
14 None.gif             </ tr >
15 None.gif             < tr >
16 None.gif                 < td style = " width: 101px " >
17 None.gif                     < asp:Label ID = " Label2 "  runat = " server "  Text = " 邮件主题: " ></ asp:Label ></ td >
18 None.gif                 < td >
19 None.gif                     < asp:TextBox ID = " TBSubject "  runat = " server " ></ asp:TextBox ></ td >
20 None.gif             </ tr >
21 None.gif             < tr >< td colspan = " 2 " >
22 None.gif                 < asp:FileUpload ID = " FileUpload1 "  runat = " server "  Width = " 259px "   /></ td >
23 None.gif             </ tr >
24 None.gif             < tr >
25 None.gif                 < td colspan = " 2 " >
26 None.gif                     < asp:Label ID = " Label3 "  runat = " server "  Text = " 邮件正文: " ></ asp:Label ></ td >
27 None.gif             </ tr >
28 None.gif             < tr >
29 None.gif                 < td colspan = " 2 " >
30 None.gif                     < asp:TextBox ID = " TBDescript "  runat = " server "  Height = " 97px "  TextMode = " MultiLine "  Width = " 247px " ></ asp:TextBox ></ td >
31 None.gif             </ tr >
32 None.gif             < tr >
33 None.gif                 < td colspan = " 2 " >
34 None.gif                     < asp:Button ID = " BSend "  runat = " server "  OnClick = " BSend_Click "  Text = " 发送 "   /></ td >
35 None.gif             </ tr >
36 None.gif         </ table >     
37 None.gif     </ div >
38 None.gif     </ form >
39 None.gif </ body >

好了,发送邮件的功能就实现了,试一下.是不是比较方便.