1、前端.aspx
01.
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeFile=
"Default.aspx.cs"
Inherits=
"_Default"
%>
02.
03.
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
04.
05.
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
06.
<head runat=
"server"
>
07.
<title>發送Email示例</title>
08.
</head>
09.
<body>
10.
<form id=
"form1"
runat=
"server"
>
11.
<h1>發送Email示例</h1>
12.
<p>發件郵箱:<asp:TextBox ID=
"txtFrom"
runat=
"server"
Width=
"407px"
Text=
"niunan@niunan.net"
></asp:TextBox>
13.
</p>
14.
<p>用戶名:<asp:TextBox ID=
"txtName"
runat=
"server"
Width=
"407px"
Text=
"niunan@niunan.net"
></asp:TextBox>
15.
</p>
16.
<p>密碼:<asp:TextBox ID=
"txtPWD"
runat=
"server"
Width=
"407px"
Text=
"nn13607886582"
></asp:TextBox>
17.
</p>
18.
<p>SMTP地址:<asp:TextBox ID=
"txtSMTP"
runat=
"server"
Width=
"407px"
Text=
"smtp.qq.com"
></asp:TextBox>
19.
</p>
20.
<p>收件郵箱:<asp:TextBox ID=
"txtTo"
runat=
"server"
Width=
"407px"
Text=
"164423073@qq.com"
></asp:TextBox>
21.
</p>
22.
<p>標題:<asp:TextBox ID=
"txtTitle"
runat=
"server"
Width=
"407px"
Text=
"test title"
></asp:TextBox>
23.
</p>
24.
<p>內容:</p>
25.
<p>
26.
<asp:TextBox ID=
"txtContent"
Text=
"test content"
TextMode=
"MultiLine"
runat=
"server"
Height=
"125px"
Width=
"432px"
></asp:TextBox>
27.
</p>
28.
<p>
29.
<asp:Button ID=
"btnSend"
runat=
"server"
Text=
"發送"
onclick=
"btnSend_Click"
/>
30.
31.
<asp:Label ID=
"lblMes"
ForeColor=
"Red"
runat=
"server"
Text=
""
></asp:Label>
32.
</p>
33.
</form>
34.
</body>
35.
</html>
2、后臺.cs
01.
using System;
02.
using System.Collections.Generic;
03.
using System.Linq;
04.
using System.Web;
05.
using System.Web.UI;
06.
using System.Web.UI.WebControls;
07.
using System.Net.Mail;
08.
using System.Net;
09.
10.
public partial class _Default : System.Web.UI.Page
11.
{
12.
protected void Page_Load(object sender, EventArgs e)
13.
{
14.
15.
}
16.
17.
/// <summary>
18.
/// 發送email,默認是25端口
19.
/// </summary>
20.
/// <param name="title">郵件標題</param>
21.
/// <param name="body">郵件內容</param>
22.
/// <param name="toAdress">收件人</param>
23.
/// <param name="fromAdress">發件人</param>
24.
/// <param name="userName">發件用戶名</param>
25.
/// <param name="userPwd">發件密碼</param>
26.
/// <param name="smtpHost">smtp地址</param>
27.
private string sendMail(string title, string body, string toAdress, string fromAdress,
28.
string userName, string userPwd, string smtpHost)
29.
{
30.
try
31.
{
32.
MailAddress to =
new
MailAddress(toAdress);
33.
MailAddress from =
new
MailAddress(fromAdress);
34.
System.Net.Mail.MailMessage message =
new
System.Net.Mail.MailMessage(from, to);
35.
message.IsBodyHtml =
true
;
// 如果不加上這句那發送的郵件內容中有HTML會原樣輸出
36.
message.Subject = title; message.Body = body;
37.
SmtpClient smtp =
new
SmtpClient();
38.
smtp.UseDefaultCredentials =
true
;
39.
smtp.Port = 25;
40.
smtp.Credentials =
new
NetworkCredential(userName, userPwd);
41.
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
42.
smtp.Host = smtpHost;
43.
message.To.Add(toAdress);
44.
smtp.Send(message);
45.
return
"郵件發送成功!"
;
46.
}
47.
catch
(Exception ex)
48.
{
49.
return
ex.Message;
50.
}
51.
}
52.
53.
protected void btnSend_Click(object sender, EventArgs e)
54.
{
55.
string title = txtTitle.Text.Trim();
56.
string content = txtContent.Text.Trim();
57.
string to = txtTo.Text.Trim();
58.
string from = txtFrom.Text.Trim();
59.
string username = txtName.Text.Trim();
60.
string pwd = txtPWD.Text.Trim();
61.
string smtp = txtSMTP.Text.Trim();
62.
63.
lblMes.Text = sendMail(title, content, to, from, username, pwd, smtp);
64.
}
65.
}