我試圖將一個自定義文件附加到電子郵件沒有用。下面的代碼調用創建加密文件的批處理文件,然後嘗試將該文件附加到電子郵件。該批處理文件成功創建文件,但是當它試圖附加時說該文件不存在。我讀過,你需要創建一個內存緩衝區或附加到相同的線程,我試着用它思考,但我現在真的很難過。任何人都可以協助C#:將一個自定義文件(.sl)附加到一個電子郵件
代碼:
string file = @"C:\EncryptedFile\file.sl";
//EXECUTE BATCHFILE SUPPLYING PARAMETERS TO IT
Process process = new Process();
process.StartInfo.Arguments = string.Format("{0} {1} {2} {3}",
file,
key,
doc1,
doc2);
process.StartInfo.FileName = MyBatchFile;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
//attach to email
Attachment attachment;
attachment = new Attachment(file);
mail.Attachments.Add(attachment);
錯誤:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\EncryptedFile\file.sl'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at 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)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType) at System.Net.Mail.AttachmentBase..ctor(String fileName) at System.Net.Mail.Attachment..ctor(String fileName)
謝謝!
通過將調試點(暫停批處理文件執行)檢查到附件代碼被調用時文件已存在嗎? 也,我會建議創建兩個批處理文件。一個是創建該文件,另一個是將其附加到郵件中。您可以在第一個批處理文件執行結束時調用第二個批處理文件,等待文件沒有正確創建。 –