接收邮件的时候在保存附件的时候报错。执行到att.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(".") + mailPath);这段代码的时候报错。报的错误:Win32 Error.  Code: 5.
拒绝访问。

解决方案 »

  1.   

    System.Web.HttpContext.Current.Server.MapPath(".") + mailPath打印下这个路径出来,看路径是否正确。然后看看文件夹是不是有写入的权限
      

  2.   

    先检查下HttpContext.Current.Server.MapPath(".") + mailPath 的路径是否正确,下面代码你可以参考一下:try
                                    ...{                                    //Random TempNameInt = new Random();
                                        //string NewMailDirName = TempNameInt.Next(100000000).ToString();
                                        //生成随机文件后缀名 
                                        string strSaveDir = "\AttachFiles\";
                                        //取得文件名(抱括路径)里最后一个"."的索引
                                        int intExt = attname.LastIndexOf(".");
                                        //取得文件扩展名
                                        string strExt = attname.Substring(intExt);
                                        //取得文件名(不包括路径)
                                        Random objRand = new Random();
                                        System.DateTime date = DateTime.Now;
                                        //生成随机文件名 
                                        string str = attname.Substring(1, attname.LastIndexOf(".") - 1);
                                        string saveName = System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString() + Convert.ToString(objRand.Next(99) * 97 + 100);
                                        string strNewName = str + "(" + saveName + ")" + strExt;//取新名字,防止重复                                    Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(".") + "\AttachFiles\" + strNewName);                                    string mailPath = strSaveDir + strNewName;                                    att.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(".") + mailPath);
                                        //获取该封邮件在数据库的ID,以便和附件信息相对应,取邮件表中的最大ID即可 
                                        int mailID = this.GetMailID();
                                        /**/////将附件信息存入数据库
                                        this.AttExtSend(mailID, attname, att.Size, mailPath, Msg.From);
                                    }
                                    catch (Exception ex)
                                    ...{
                                        throw new Exception(ex.Message);
                                    }