用asp.net发送电子邮件

    .net提供了强大的功能,其中就包括发送邮件上.经过仔细研究才发现用.net发送电子邮件是很简单的一件事情.现在我们就来看一下.
    首先添加命名空间
None.gif Using System.Net.Mail
下面我们看发送邮件的代码部分:( 注意:网易的邮件服务器是smtp.163.com)
None.gif MailAddress 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格式发送邮件)
下面是对页面的设置:
 1 None.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 >

好了,发送邮件的功能就实现了,试一下.是不是比较方便.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值