本文實例為大家分享了ASP.NET郵件發(fā)送案例,供大家參考,具體內(nèi)容如下
1、前臺頁面 SendEmail.aspx 代碼
<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"> 郵件內(nèi)容: </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代碼
protected void btn_SendEmail_Click(object sender, EventArgs e) { //聲明一個Mail對象 MailMessage mymail = new MailMessage(); //發(fā)件人地址 //如是自己,在此輸入自己的郵箱 mymail.From = new MailAddress("15510180880@163.com"); //收件人地址 mymail.To.Add(new MailAddress(tb_Email.Text)); //郵件主題 mymail.Subject = tb_Subject.Text; //郵件標題編碼 mymail.SubjectEncoding = System.Text.Encoding.UTF8; //發(fā)送郵件的內(nèi)容 mymail.Body = tb_Body.Text; //郵件內(nèi)容編碼 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郵箱內(nèi)
3.3、QQ郵箱內(nèi)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com