学习记录 用C#.net 加 asp.net发送带附件邮件

必须using这两个库

using System.Net;

using System.Net.Mail;


某个按钮下的语句

protected void BtnSendAEmail_Click(object sender, EventArgs e)
    {
        SmtpClient SendClient =new SmtpClient();                                   //新SmtpClient对象,使用163的SMTP服务器
        SendClient.Host ="smtp.163.com";
        SendClient.Credentials = new NetworkCredential("例如[email protected]", "认证密码");    //必须到自己信箱中开启smtp
        string FilePath;
        FilePath = Server.MapPath("");                                             //映射路径,不使用固定编码路径
        Attachment MyAttchment = new Attachment(FilePath + "\\MailSendTest.txt");  //附件对象
       
        MailMessage WantSendMail=new MailMessage();                                //邮件对象
        WantSendMail.From = new MailAddress("[email protected]");
        WantSendMail.To.Add("[email protected]");
        WantSendMail.Subject = "标题";
        WantSendMail.Body = "主体";
        WantSendMail.Attachments.Add( MyAttchment );

        SendClient.Send(WantSendMail);
        WantSendMail.Attachments.Dispose();                                                    //必须释放,否则无法编辑MailSendTest.txt
    }


你可能感兴趣的:(学习记录 用C#.net 加 asp.net发送带附件邮件)