(五)电子邮件模块(SMTP协议发送带附件的邮件及抄送)

本文介绍了如何在ASP.NET中使用SMTP协议发送带有附件的邮件,特别指出使用`Attachment`构造函数时应注意的问题。作者分享了解决找不到文件路径的技巧,避免将上传文件保存到服务器程序文件夹,而是直接使用文件流创建附件。同时,还提到了处理大附件时可能遇到的HTTP 404.13错误,需要修改配置文件`<httpRuntime>`和`<system.webServer>`节点以增加最大请求长度。
摘要由CSDN通过智能技术生成

以下是我的代码

email.aspx.cs

namespace email
{
    public partial class smtp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {         
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MailMessage mymail=new MailMessage();//声明一个mail对象
            mymail.From=new MailAddress("XXXX@163.com","your name");//发件人地址
            mymail.To.Add(new MailAddress(TextBox1.Text));//收件人地址
            mymail.Subject=TextBox2.Text;//邮件主题
            mymail.Body=TextBox3.Text;//发送邮件的内容
          //  string filename = Path.GetFileName(FileUpload1.FileName);//获得文件名和后缀        
            //FileUpload1.SaveAs(Server.MapPath("~/") + filename)//保存文件到本程序的文件夹下
            Attachment myfiles = new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName);//把上传的文件指向的文件流给附件对象
            mymail.Attachments.Add(myfiles);//发送邮件附件使用Attachments
            mymail.CC.Add(new MailAddress(TextBox4.Text));//抄送邮件用CC
            SmtpClient myclient=new SmtpClient();//创建一个邮件服务器
            myclient.Host="smtp.163.com";//使用的163的SMTP服务器
            myclient.Port=25;//smtp服务器端口
            myclient.Credentials=new NetworkCredential("XXXX@163.com", "XXXX");//网易的邮箱及密码
            myclient.Send(mymail);
            Response.Write("成功!");
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
        }
      }
    }   </
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值