- 前台頁面 SendEmail.aspx 代碼
1 <h2>
2 發送電子郵件演示
3 </h2>
4 <table cellpadding="0" cellspacing="0" border="0" style="font-family: 宋體, Arial, Helvetica, sans-serif;
5 font-size: 15px; width: 411px;">
6 <tr>
7 <td class="style5">
8 郵箱地址:
9 </td>
10 <td class="style6">
11 <asp:TextBox ID="tb_Email" runat="server" Width="269px"></asp:TextBox>
12 </td>
13 </tr>
14 <tr>
15 <td class="style5">
16 抄送至:
17 </td>
18 <td class="style6">
19 <asp:TextBox ID="tb_cc" runat="server" Width="268px"></asp:TextBox>
20 </td>
21 </tr>
22 <tr>
23 <td class="style5">
24 郵件主題:
25 </td>
26 <td class="style6">
27 <asp:TextBox ID="tb_Subject" runat="server" Width="268px"></asp:TextBox>
28 </td>
29 </tr>
30 <tr>
31 <td class="style5">
32 郵件內容:
33 </td>
34 <td class="style6">
35 <asp:TextBox ID="tb_Body" runat="server" Height="63px" TextMode="MultiLine" Width="266px"></asp:TextBox>
36 </td>
37 </tr>
38 <tr>
39 <td class="style5">
40 添加附件:
41 </td>
42 <td class="style6">
43 <asp:FileUpload ID="tb_Attachment" runat="server" Width="265px" />
44 </td>
45 </tr>
46 <tr>
47 <td align="right" colspan="2">
48 <asp:Button ID="btn_SendEmail" runat="server" Text="發送郵件" OnClick="btn_SendEmail_Click" />
49 </td>
50 </tr>
51 </table> - 后台SendEmail.aspx.cs代碼
1 protected void btn_SendEmail_Click(object sender, EventArgs e)
2 {
3 //聲明一個Mail對象
4 MailMessage mymail = new MailMessage();
5 //發件人地址
6 //如是自己,在此輸入自己的郵箱
7 mymail.From = new MailAddress("15510180880@163.com");
8 //收件人地址
9 mymail.To.Add(new MailAddress(tb_Email.Text));
10 //郵件主題
11 mymail.Subject = tb_Subject.Text;
12 //郵件標題編碼
13 mymail.SubjectEncoding = System.Text.Encoding.UTF8;
14 //發送郵件的內容
15 mymail.Body = tb_Body.Text;
16 //郵件內容編碼
17 mymail.BodyEncoding = System.Text.Encoding.UTF8;
18 //添加附件
19 Attachment myfiles = new Attachment(tb_Attachment.PostedFile.FileName);
20 mymail.Attachments.Add(myfiles);
21 //抄送到其他郵箱
22 mymail.CC.Add(new MailAddress(tb_cc.Text));
23 //是否是HTML郵件
24 mymail.IsBodyHtml = true;
25 //郵件優先級
26 mymail.Priority = MailPriority.High;
27 //創建一個郵件服務器類
28 SmtpClient myclient = new SmtpClient();
29 myclient.Host = "SMTP.163.com";
30 //SMTP服務端口
31 myclient.Port = 25;
32 //驗證登錄
33 myclient.Credentials = new NetworkCredential("@@@@@@", "*****");//"@"輸入有效的郵件名, "*"輸入有效的密碼
34 myclient.Send(mymail);
35 } - 效果如下
3.1、如下圖填入各項,點擊發送郵件。
3.2、163郵箱內。
3.3、QQ郵箱內。