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>");
        }
    }
}


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

原文地址:https://www.cnblogs.com/shuai/p/1571364.html