论坛: 菜鸟乐园 标题: ★请关注:ASP.net发邮件的例程! 复制本贴地址    
作者: cjsh05 [cjsh05]    论坛用户   登录
★请关注:ASP.net发邮件的例程 !
   
    大家好,我是Asp.net的忠实爱好者,很想把这方面的经验和大家 分享一下. 目前我选用的是时代互联( http://www.now.net.cn ) 的Asp.net空间,欢迎大家一起进来讨论讨论!
   
    -------------------------------------------------------
   
    ASP.net发邮件的例程:
   
    <%@ page Language = "C#" debug = "true" Explicit="True"
   
    %>
    <%@ Import Namespace = "System.Web.Util" %>
    <%@ Import Namespace = "System.Web.Mail" %>
    <script language = "C#" runat = "server">
   
    public void SendMail (Object Obj, EventArgs E) {
    labelSendMailResult.Text = "";
    if (Page.IsValid) {
    MailMessage mailObj = new MailMessage();
   
    // 设置email的'from'和'to'的地址
    mailObj.From = inputMailFrom.Value;
    mailObj.To = inputMailTo.Value;
   
    mailObj.Subject = inputMailSubject.Value;
    mailObj.Body = textBoxMailBody.Text;
   
    // 可选: 使用html格式的Email
    mailObj.BodyFormat = MailFormat.Html;
   
    // 可选: 对邮件进行加密
    // mailObj.BodyEncoding = MailFormat.Base64;
   
    // 可选: 设置邮件的优先级别为高
    mailObj.Priority = MailPriority.High;
   
    // 可选: 附件
    if (inputMailAttachment.PostedFile.ContentLength > 0) {
    // 注意这里我们创建了一个MailAttachment对象来附加一个文
   
    件到email。
    mailObj.Attachments.Add(new
   
    MailAttachment(inputMailAttachment.PostedFile.FileName)
   
    );
    }
   
    // 使用SmtpMail对象来发送邮件。
    SmtpMail.Send(mailObj);
    labelSendMailResult.Text = "邮件发送成功 From: " +
   
    inputMailFrom.Value + "; To: " + inputMailTo.Value;
    if (inputMailAttachment.PostedFile.ContentLength > 0) {
    labelSendMailResult.Text += "<br>该邮件包含附件: " +
   
    inputMailAttachment.PostedFile.FileName + ", 附件大小为
   
    : " + (inputMailAttachment.PostedFile.ContentLength /
   
    1024).ToString() + " K Byte(s)";
    }
    }
    }
    </script>
   
   
    <html>
    <head>
    <title>
    发送邮件 ASP.NET</title>
    </head>
    <body>
    <div align="center">
    <table width="100%" border="0" cellpadding="0"
   
    cellspacing="0" style="border-collapse: collapse"
   
    bordercolor="#eeeeee" id="AutoNumber1">
    <form id="formMail" method="post" action=""
   
    enctype="multipart/form-data" runat="server">
    <tr>
    <td width="20%" height="24">收件人地址: </td>
    <td width="80%" height="24">
    <input type="text" id="inputMailTo" name="inputMailTo"
   
    runat="server" size="48">
    <asp:RequiredFieldValidator
    id="RequiredFieldValidatorInputMailTo"
    ControlToValidate="inputMailTo"
    Display="Static"
    EnableClientScript="False"
    ErrorMessage="收件人不能为空"
    runat="server"/>
    <asp:RegularExpressionValidator
   
    id="RegularExpressionValidatorInputMailTo"
    ControlToValidate="inputMailTo"
    ValidationExpression="^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$"
    Display="Static"
    EnableClientScript="false"
    ErrorMessage="收件人邮件地址错误"
    runat="server"/>
    </td>
    </tr>
    <tr>
    <td width="20%" height="24">发件人地址: </td>
    <td width="80%" height="24">
    <input type="text" id="inputMailFrom"
   
    name="inputMailFrom" runat="server" size="48">
    <asp:RequiredFieldValidator
    id="RequiredFieldValidatorInputMailFrom"
    ControlToValidate="inputMailFrom"
    Display="Static"
    EnableClientScript="False"
    ErrorMessage="发件人不能为空"
    runat="server"/>
    <asp:RegularExpressionValidator
   
    id="RegularExpressionValidatorInputMailFrom"
    ControlToValidate="inputMailFrom"
    ValidationExpression="^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$"
    Display="Static"
    EnableClientScript="false"
    ErrorMessage="发件人邮件地址错误"
    runat="server"/>
    </td>
    </tr>
    <tr>
    <td width="20%" height="24">邮件主题: </td>
    <td width="80%" height="24">
    <input type="text" id="inputMailSubject"
   
    name="inputMailSubject" runat="server" size="48">
    <asp:RequiredFieldValidator
    id="RequiredFieldValidatorInputMailSubject"
    ControlToValidate="inputMailSubject"
    Display="Static"
    EnableClientScript="False"
    ErrorMessage="邮件主题不能为空"
    runat="server"/>
    </td>
    </tr>
    <tr>
    <td width="20%" height="24">邮件内容:
    </td>
    <td width="80%" height="24">
    <asp:TextBox id="textBoxMailBody"
   
    name="textBoxMailBody" runat="server" Rows="6"
   
    Cols="48" TextMode="MultiLine" />
    <asp:RequiredFieldValidator
    id="RequiredFieldValidatorTextBoxMailBody"
    ControlToValidate="textBoxMailBody"
    Display="Static"
    EnableClientScript="False"
    ErrorMessage="邮件内容不能为空"
    runat="server"/>
    </td>
    </tr>
    <tr>
    <td width="20%" height="24">邮件附件:
    </td>
    <td width="80%" height="24">
    <input type="file" id="inputMailAttachment"
   
    name="inputMailAttachment" runat="server" size="48">
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
    <input type="button" value="Send Mail"
   
    OnServerClick="SendMail" id="buttonSendMail"
   
    name="buttonSendMail" runat="server">
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center" height="24">
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
    <asp:Label id="labelSendMailResult" runat="server"
   
    text="" font-bold="True" forecolor="#FF0000"/>
    </td>
    </tr>
    </form>
    </table>
    </div>
    </body>
    </html>
   
   
   
    使用详解和例程: http://www.now.net.cn/support/host/ (这 里有更多更细的Asp.net的详细例程)
   
    如果数据库是使用 SQLSERVER 2000 (加上:ACCESS ),就推荐使用 商务型C ,空间的具体型号和配置,
    详情请点击: http://now.net.cn/host/commerce.net?PHPSESSID=8d63dcae363e28398da69fd1e85fafa9
   
Asp.net虚拟主机的服务提供商中,目前首推时代互联(www.now.net.cn), 因为他们是行业中,最早推出 ASP.NET 的虚拟主机提供商,同时也是最
    早推出 JSP(JAVA) ,ASP+PHP ,JSP+PHP ,等混合型的高质量虚拟主机。
    更多主机请点击:http://now.net.cn/host/
   
    时代互联(www.now.net.cn) 的虚拟主机,免费配有 CDN 镜象网络加速 ,送域名 邮箱,流量访问系统,留言版,计数器!
      全球加速网络镜像CDN技术实例:
    CDN客户实例:
    http://www.huihuicheng.com
    http://www.0756.sh
    http://www.zcan.cn
    http://www.0395h.com
    http://www.rubberpowder.com
    http://www.fabar.cn
    http://www.shelie.com/
    http://www.fableage.com
    http://www.szhisea.com
    http://www.kingstar.biz
    http://www.jianlv.cn
    http://www.qianju.cn
    http://www.molongjy.com/
    商务型及以上空间者可免费获赠GCDN全球网络镜像及加速引擎,

    如有问题要咨询,时代互联提供免费咨询电话,点击这里: http://www.now.net.cn/customer/moreline.net
   
   
   

地主 发表时间: 05-03-26 11:35

论坛: 菜鸟乐园

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号