为什么此电子邮件附件在我的asp.net服务器上失败,但在本地主机上失败?

为什么此电子邮件附件在我的asp.net服务器上失败,但在本地主机上失败?,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,这段代码在localhost/iisexpress上非常有效。我收到附有pdf的电子邮件。但是,当我将它复制到我的服务器IIS7时,它失败了。你知道为什么吗 var db = new DirectCellarsModel(); var status = false; MailAddress from = new MailAddress(EMAIL_SENDER); MailAddress to = new MailAd

这段代码在localhost/iisexpress上非常有效。我收到附有pdf的电子邮件。但是,当我将它复制到我的服务器IIS7时,它失败了。你知道为什么吗

   var db = new DirectCellarsModel();
            var status = false;
            MailAddress from = new MailAddress(EMAIL_SENDER);
            MailAddress to = new MailAddress(recipient);
            MailMessage message = new MailMessage(from, to);
            message.Subject = subject;
            message.Body = messageBody;

            if (pdf)
            {
                Attachment data = new Attachment(@"C:\develop\PDF\myfree.pdf", MediaTypeNames.Application.Octet);
                message.Attachments.Add(data);
            }

            SmtpClient client = new SmtpClient(SMTP_CLIENT);
这是错误消息:

The device is not ready.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: The device is not ready.


Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


IOException: The device is not ready.

   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +1093
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +886
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +127
   System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType) +104

错误
设备未就绪
通常表示给定的位置不存在。通常这意味着驱动器本身不存在,但在这种情况下,我无法想象服务器没有C驱动器(但可能没有,服务器可以从其他设备启动)。检查以确保驱动器和驱动器上的文件夹确实存在

请检查此答案,以了解有关调试和其他可能问题的提示:

如果该位置确实存在,请检查用户是否具有读取该位置的权限。即使服务器上存在位置“C:\develop\PDF\”,您的IIS\U用户帐户(或其他帐户)所使用的帐户也可能没有访问该位置的完全权限,匿名用户仍由
IUser

标识


您可能想考虑在网站的根目录中设置一个子文件夹,IIS帐户的相关权限可以轻松访问。p> 你能发布错误消息吗?对不起,我是故意的,只是忘了。尝试加载pdf文件时出现“设备未就绪”。好的。。。问题解决了。我重新编译了本地开发箱中的所有内容,并将其重新部署到服务器上。经过5-6次尝试后,它现在已经开始工作了。。。不知道为什么,但是,嘿,它工作了…文件的路径呢?硬编码完整路径从来不是一个好主意。