asp.net2.0實現郵件發送(測試成功)

來源:互聯網
上載者:User

1、Default.aspx代碼如下: 複製代碼 代碼如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>

<!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 runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="TABLE1" runat="server" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 393px">
收信:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
主題:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
內容:<asp:TextBox ID="TextBox3" runat="server" Height="154px" TextMode="MultiLine"
Width="336px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="發送" OnClick="Button1_Click" /></td>
</tr>
</table>

</div>
<table id="Table2" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false">
<tr>
<td align="center" style="width: 400px">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="恭喜您,發表成功!"></asp:Label><br />
<asp:Button ID="Button2" runat="server" Text="返回" OnClick="Button2_Click" /></td>
</tr>
</table>
</form>
</body>
</html>

2、Default.aspx.cs代碼如下: 複製代碼 代碼如下: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;
using System.Net.Mail;

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

}
protected void Button1_Click(object sender, EventArgs e)
{
////設定寄件者信箱,及顯示名字
MailAddress from = new MailAddress("zgdx0503@cpp114.com", "延邊職大資訊中心");
//設定收件者信箱,及顯示名字
MailAddress to = new MailAddress(TextBox1.Text, "0503班");
//建立一個MailMessage對象
MailMessage oMail = new MailMessage(from, to);

oMail.Subject = TextBox2.Text; //郵件標題
oMail.Body = TextBox3.Text; //郵件內容

oMail.IsBodyHtml = true; //指定郵件格式,支援HTML格式
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//郵件採用的編碼
oMail.Priority = MailPriority.High;//設定郵件的優先順序為高

//發送郵件伺服器
SmtpClient client = new SmtpClient();
client.Host = "mail.cpp114.com"; //指定郵件伺服器
client.Credentials = new NetworkCredential("zgdx0503@cpp114.com", "123456");//指定伺服器郵件,及密碼

//發送
try
{
client.Send(oMail); //發送郵件
Label1.Text = "恭喜你!郵件發送成功。";
}
catch
{
Label1.Text = "郵件發送失敗,檢查網路及信箱是否可用。";
}

oMail.Dispose(); //釋放資源

TABLE1.Visible = false;
Table2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
//返回,繼續發送
Response.Redirect(Request.Url.ToString());
TABLE1.Visible = true;
Table2.Visible = false;
}
}

3、運行並輸入測試信箱zgdx0503@tom.com,(密碼:123456)。如下所示:

4、開啟信箱查看

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.