前台頁面 SendEmailaspx 代碼
<h>
發送電子郵件演示
</h>
<table cellpadding= cellspacing= border= >
<tr>
<td class=style>
郵箱地址
</td>
<td class=style>
<asp:TextBox ID=tb_Email runat=server Width=px></asp:TextBox>
</td>
</tr>
<tr>
<td class=style>
抄送至
</td>
<td class=style>
<asp:TextBox ID=tb_cc runat=server Width=px></asp:TextBox>
</td>
</tr>
<tr>
<td class=style>
郵件主題
</td>
<td class=style>
<asp:TextBox ID=tb_Subject runat=server Width=px></asp:TextBox>
</td>
</tr>
<tr>
<td class=style>
郵件內容
</td>
<td class=style>
<asp:TextBox ID=tb_Body runat=server Height=px TextMode=MultiLine Width=px></asp:TextBox>
</td>
</tr>
<tr>
<td class=
style
>
添加附件
</td>
<td class=
style
>
<asp:FileUpload ID=
tb_Attachment
runat=
server
Width=
px
/>
</td>
</tr>
<tr>
<td align=
right
colspan=
>
<asp:Button ID=
btn_SendEmail
runat=
server
Text=
發送郵件
OnClick=
btn_SendEmail_Click
/>
</td>
</tr>
</table>
? 後台SendEmail
aspx
cs代碼
protected void btn_SendEmail_Click(object sender
EventArgs e)
{
//聲明一個Mail對象
MailMessage mymail = new MailMessage()
//發件人地址
//如是自己
在此輸入自己的郵箱
mymail
From = new MailAddress(
)
//收件人地址
mymail
To
Add(new MailAddress(tb_Email
Text))
//郵件主題
mymail
Subject = tb_Subject
Text;
//郵件標題編碼
mymail
SubjectEncoding = System
Text
Encoding
UTF
;
//發送郵件的內容
mymail
Body = tb_Body
Text;
//郵件內容編碼
mymail
BodyEncoding = System
Text
Encoding
UTF
;
//添加附件
Attachment myfiles = new Attachment(tb_Attachment
PostedFile
FileName)
mymail
Attachments
Add(myfiles)
//抄送到其他郵箱
mymail
CC
Add(new MailAddress(tb_cc
Text))
//是否是HTML郵件
mymail
IsBodyHtml = true;
//郵件優先級
mymail
Priority = MailPriority
High;
//創建一個郵件服務器類
SmtpClient myclient = new SmtpClient()
myclient
Host =
;
//SMTP服務端口
myclient
Port =
;
//驗證登錄
myclient
Credentials = new NetworkCredential(
@@@@@@
*****
)
//
@
輸入有效的郵件名
*
輸入有效的密碼
myclient
Send(mymail)
}
? 效果如下
如下圖填入各項點擊發送郵件
郵箱內
QQ郵箱內
From:http://tw.wingwit.com/Article/program/net/201311/13912.html