2017-05-21 21 views
0

這段代碼在localhost/IIS express上完美工作。我收到附帶pdf的電子郵件。但是,當我將它複製到我的服務器IIS7時,它會失敗。任何想法爲什麼?爲什麼此電子郵件附件在我的asp.net服務器上失敗,但不在本地主機上?

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 
+0

你能發佈錯誤信息嗎? –

+0

對不起,只是想忘記。試圖加載PDF文件時,「設備未準備就緒」。 –

+0

好的......問題解決了。我重新編譯了本地開發框中的所有內容,並將其重新部署到服務器。它已經開始工作,經過5-6次嘗試......不知道爲什麼,但是嘿,它的工作...... –

回答

0

誤差Device Not Ready通常表明給定的位置不存在。通常這意味着驅動器本身不存在,但在這種情況下,我無法確定服務器沒有C驅動器(但它可能不是,服務器可以從另一個設備啓動)。檢查以確保驅動器和驅動器上的文件夾確實存在。

檢查這個答案,以及有關調試的提示和其他可能的問題:

'device not ready' while using File.Copy() in C#

如果該位置不存在,檢查用戶有權讀取它。匿名用戶由IUser標識。

+0

它確實存在。我已經驗證了很多次。 –

+0

該文件的路徑怎麼樣?對完整路徑進行硬編碼絕不是一個好主意。 – krillgar

+0

我加入到我的答案.... –

0

即使服務器上存在「C:\ develop \ PDF \」位置,您的IIS_USER帳戶(或其他用戶)可能沒有完全訪問該位置的權限。

您可能需要考慮在您的網站的根目錄下設置一個子文件夾,並使用IIS帳戶的相關權限輕鬆訪問。

相關問題