• asp.net发送邮件


          ASP.NET提供了功能强大的邮件发送功能。.net类库中名为System.Web.Mail的命名空间提供了3种不同的类,用于实现.NET平台下的电子邮件发送功能。它们很好的封装了SMTP协议的通讯模型、命令应答。
          下面是用ASP.NET发送邮件的一个实例:
    前台代码:
    <body>
        <form id="form1" runat="server">
        <div style="text-align: center">
            发送邮件附件<br />
            <br />
            <br />
            <table style=" 677px">
                <tr>
                    <td colspan="2" style="text-align: center">
                        功能强大的邮件发送</td>
                </tr>
                <tr>
                    <td style=" 274px; height: 29px; text-align: left">
                        收件人:</td>
                    <td style="height: 29px; text-align: left">
                        <asp:TextBox ID="tbTo" runat="server" Width="349px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style=" 274px; height: 30px; text-align: left">
                        发件人:</td>
                    <td style="height: 30px; text-align: left">
                        <asp:TextBox ID="tbFrom" runat="server" Width="349px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style=" 274px; height: 27px; text-align: left">
                        邮件主题:</td>
                    <td style="height: 27px; text-align: left">
                        <asp:TextBox ID="tbSubject" runat="server" Width="349px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style=" 274px; height: 32px; text-align: left">
                        优先级:<asp:DropDownList ID="ddlPriority" runat="server">
                            <asp:ListItem Value="High">高</asp:ListItem>
                            <asp:ListItem Value="Normal">普通</asp:ListItem>
                            <asp:ListItem Value="Low">低</asp:ListItem>
                        </asp:DropDownList></td>
                    <td style="height: 32px; text-align: left">
                        邮件格式:<asp:DropDownList ID="ddlBodyFormat" runat="server">
                            <asp:ListItem Value="Text">文本格式</asp:ListItem>
                            <asp:ListItem Value="Html">HTML</asp:ListItem>
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: left">
                        邮件内容:</td>
                </tr>
                <tr>
                    <td colspan="2" style="height: 100px; text-align: left">
                        <asp:TextBox ID="tbBody" runat="server" Height="89px" TextMode="MultiLine" Width="585px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style=" 274px; height: 26px; text-align: left">
                        邮件附件:</td>
                    <td style="height: 26px; text-align: left">
                        <asp:FileUpload ID="AttachFile" runat="server" Width="369px" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="height: 31px">
                        <asp:Button ID="BtnSend" runat="server" Text="发送" Width="73px" OnClick="BtnSend_Click" /></td>
                </tr>
            </table>
       
        </div>
        </form>
    </body>
    后台代码:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Web.Mail;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void BtnSend_Click(object sender, EventArgs e)
        {
            //创建MailMessage对象
            MailMessage MyMsg = new MailMessage();
            MyMsg.From = tbFrom.Text;
            MyMsg.To = tbTo.Text;
            MyMsg.Subject = this.tbSubject.Text;
            MyMsg.Priority =(MailPriority)this.ddlPriority.SelectedIndex;
            MyMsg.BodyFormat = (MailFormat)this.ddlBodyFormat.SelectedIndex;
            MyMsg.Body = tbBody.Text;
            //如果有附件上传
            HttpPostedFile hpfFile = this.AttachFile.PostedFile;
            if (hpfFile.FileName != "")
            {
            //有附件,则上传到Temp目录中
                //取得文件名(不含路径)
                char[] de ={ '\\' };
                string[] AFileName = hpfFile.FileName.Split(de);
                string strFilename = AFileName[AFileName.Length - 1];
                string strPath = Server.MapPath(".") + "http://www.cnblogs.com/shuai/admin/file://temp//" + strFilename;
                hpfFile.SaveAs(strPath);
                //添加附件
                MyMsg.Attachments.Add(new MailAttachment(strPath));
            }
            try
            {
            //发送
                SmtpMail.Send(MyMsg);
                Response.Write("<script language='javascript'>alert('发送成功!')</script>");
                tbTo.Text = "";
                tbSubject.Text = "";
                tbFrom.Text = "";
                tbBody.Text ="";
                ddlPriority.SelectedIndex = 1;
                ddlBodyFormat.SelectedIndex = 0;

            }
            catch(Exception ee)
            {
                Response.Write("<script language='javascript'>alert('发送失败!'"+ee.ToString()+")</script>");
            }
        }
    }


          支持增加附件,也支持群发邮件。

  • 相关阅读:
    为什么有人说指针是 C 语言的精髓?
    属于编程的黄金时代结束了吗?不,这片领地的大门仍然敞开
    编程和编程语言竟然不是一回事,你知道吗?
    为什么 C 语言是程序员的首选,你知道吗?
    CRoundButton2 -一个花哨的图形按钮
    彩虹按钮
    EnableGroupboxControls -一个非mfc函数,用于启用或禁用groupbox中的所有控件
    CImageButtonWithStyle -按钮使用图像与XP视觉风格
    使用。net SDK编写位图按钮控件
    Joe的自动重复按钮类的。net端口
  • 原文地址:https://www.cnblogs.com/shuai/p/1571364.html
Copyright © 2020-2023  润新知