ASP.NET 郵件發送 System.Net.Mail

來源:互聯網
上載者:User
  1. 前台頁面 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>
  2. 後台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. 效果如下
    3.1、如填入各項,點擊發送郵件。


    3.2、163郵箱內。

    3.3、QQ郵箱內。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.