ASP.NET 邮件发送 System.Net.Mail

  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邮箱内。

posted @ 2012-03-27 14:17  XuebinDing  阅读(6800)  评论(7编辑  收藏  举报