Send Email 發送郵件

 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>How to Send Email with Authentication using ASP.NET 2.0 and C#</title>
    <style type="text/css">
<!--
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
a:link {
    color: #0000FF;
}
a:visited {
    color: #0000FF;
}
a:hover {
    color: #0000FF;
    text-decoration: none;
}
a:active {
    color: #0000FF;
    }
.basix {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
}
.header1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    font-weight: bold;
    color: #006699;
}
.lgHeader1 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 18px;
    font-weight: bold;
    color: #0066CC;
    background-color: #CEE9FF;
}
-->
</style>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <form id="form1" runat="server">
        <br />
        <table width="752" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr bgcolor="#5482FC">
                <td colspan="4">
                    <img src="/media/spacer.gif" width="1" height="1" /></td>
            </tr>
            <tr>
                <td width="1" bgcolor="#5482FC">
                    <img src="media/spacer.gif" alt="Server Intellect" width="1" height="1" /></td>
                <td width="250">
                    <a href="http://www.serverintellect.com">
                        <img src="media/logo.gif" alt="Server Intellect" width="250" height="75" border="0" /></a></td>
                <td width="500" bgcolor="#3399ff">
                    <a href="http://www.serverintellect.com">
                        <img src="media/headerR1.gif" alt="Server Intellect" width="500" height="75" border="0" /></a></td>
                <td width="1" bgcolor="#5482FC">
                    <img src="media/spacer.gif" alt="Server Intellect" width="1" height="1" /></td>
            </tr>
            <tr bgcolor="#5482FC">
                <td colspan="4">
                    <img src="media/spacer.gif" width="1" height="1" /></td>
            </tr>
        </table>
        <div>
            <br />
            <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc">
                <tr>
                    <td height="50" align="center" class="lgHeader1">
                        How to Send Email with Authentication using ASP.NET 2.0 and C#</td>
                </tr>
            </table>
            <br />
            <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">
                        &nbsp;To</td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">
                        &nbsp;From</td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtFrom" runat="server" Columns="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" bgcolor="#eeeeee" class="header1">
                        &nbsp;SMTP Server</td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtSMTPServer" runat="server" Columns="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" bgcolor="#eeeeee" class="header1">
                        SMTP User</td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtSMTPUser" runat="server" Columns="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" bgcolor="#eeeeee" class="header1">
                        SMTP Pass</td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtSMTPPass" runat="server" Columns="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">
                        &nbsp;Subject</td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">
                        &nbsp;Body</td>
                    <td bgcolor="#FFFFFF">
                        <asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" bgcolor="#eeeeee" class="header1">
                        Action</td>
                    <td bgcolor="#FFFFFF">
                        <asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" /></td>
                </tr>
                <tr>
                    <td width="100" align="right" bgcolor="#eeeeee" class="header1">
                        Status</td>
                    <td bgcolor="#FFFFFF" class="basix">
                        <asp:Literal ID="litStatus" runat="server"></asp:Literal></td>
                </tr>
            </table>
            <br />
            <table width="500" align="center" cellpadding="0" cellspacing="0">
                <tr>
                    <td height="50" align="center" class="basix">
                        <strong>Power. Stability. Flexibility.</strong><br />
                        Hosting from <a href="http://www.serverintellect.com">Server Intellect</a><br />
                        <br />
                        For more ASP.NET Tutorials visit <a href="http://www.AspNetTutorials.com">www.AspNetTutorials.com</a></td>
                </tr>
            </table>
            <br />
            <br />
            <br />
        </div>
    </form>
</body>
</html>
 

 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.Net.Mail;

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

        // This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com

        // Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {

            MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
            SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(txtSMTPUser.Text, txtSMTPPass.Text);
            emailClient.UseDefaultCredentials = false;
            emailClient.Credentials = SMTPUserInfo;
            emailClient.Send(message);
            litStatus.Text = "Message Sent";

        }
        catch (Exception ex)
        {
            litStatus.Text = ex.ToString();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章