本文實例為大家分享了ASP.NET郵件發送案例,供大家參考,具體內容如下
1、前台頁面 SendEmail.aspx 代碼
<h2> 發送電子郵件演示 </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="發送郵件" 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(); //發件人地址 //如是自己,在此輸入自己的郵箱 mymail.From = new MailAddress("[email protected]"); //收件人地址 mymail.To.Add(new MailAddress(tb_Email.Text)); //郵件主題 mymail.Subject = tb_Subject.Text; //郵件標題編碼 mymail.SubjectEncoding = System.Text.Encoding.UTF8; //發送郵件的內容 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; //郵件優先級 mymail.Priority = MailPriority.High; //創建一個郵件服務器類 SmtpClient myclient = new SmtpClient(); myclient.Host = "SMTP.163.com"; //SMTP服務端口 myclient.Port = 25; //驗證登錄 myclient.Credentials = new NetworkCredential("@@@@@@", "*****");//"@"輸入有效的郵件名, "*"輸入有效的密碼 myclient.Send(mymail); }
3、效果如下
3.1、如下圖填入各項,點擊發送郵件
3.2、163郵箱內
3.3、QQ郵箱內
以上就是本文的全部內容,希望對大家學習C#程序設計有所幫助。