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