熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

郵件發送 System.Net.Mail

2022-06-13   來源: .NET編程 

  前台頁面 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>
   
    ? 後台SendEmailaspxcs代碼
   
    protected void btn_SendEmail_Click(object sender EventArgs e)
   
             {
   
                 //聲明一個Mail對象
   
                 MailMessage mymail = new MailMessage()
   
                 //發件人地址
   
                 //如是自己在此輸入自己的郵箱
   
                 mymailFrom = new MailAddress(
   
                 //收件人地址
   
                 mymailToAdd(new MailAddress(tb_EmailText))
   
                 //郵件主題
   
                 mymailSubject = tb_SubjectText;
   
                 //郵件標題編碼
   
                 mymailSubjectEncoding = SystemTextEncodingUTF;
   
                 //發送郵件的內容
   
                 mymailBody = tb_BodyText;
   
                 //郵件內容編碼
   
                 mymailBodyEncoding = SystemTextEncodingUTF;
   
                 //添加附件
   
                 Attachment myfiles = new Attachment(tb_AttachmentPostedFileFileName)
   
                 mymailAttachmentsAdd(myfiles)
   
                 //抄送到其他郵箱
   
                 mymailCCAdd(new MailAddress(tb_ccText))
   
                 //是否是HTML郵件
   
                 mymailIsBodyHtml = true;
   
                 //郵件優先級
   
                 mymailPriority = MailPriorityHigh;
   
                 //創建一個郵件服務器類
   
                 SmtpClient myclient = new SmtpClient()
   
                 myclientHost = ;
   
                 //SMTP服務端口
   
                 myclientPort = ;
   
                 //驗證登錄


   
                 myclientCredentials = new NetworkCredential(@@@@@@ *****//@輸入有效的郵件名 *輸入有效的密碼
   
                 myclientSend(mymail)
   
             }
   
    ? 效果如下

   
    
    如下圖填入各項點擊發送郵件

  

    
    郵箱內

  

    
    QQ郵箱內


From:http://tw.wingwit.com/Article/program/net/201311/13912.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.