感謝所有,最後我做到這一點如下:1. 手動創建由Outlook模板.msg文件。 2.打開它並複製到新的流。來自intenet的許多代碼。 3.將我的信息更新到此流,並保存爲新的.msg文件。 (以下是我更新.msg文件中的附件文件時的源代碼,並且您應該從OutlookStorage.cs中獲取其他源代碼)。
//create a ILockBytes (unmanaged byte array) and then create a IStorage using the byte array as a backing store
NativeMethods.CreateILockBytesOnHGlobal(IntPtr.Zero, true, out memoryStorageBytes);
NativeMethods.StgCreateDocfileOnILockBytes(memoryStorageBytes, NativeMethods.STGM.CREATE | NativeMethods.STGM.READWRITE | NativeMethods.STGM.SHARE_EXCLUSIVE, 0, out memoryStorage);
//copy the save storage into the new storage
saveMsg.storage.CopyTo(0, null, IntPtr.Zero, memoryStorage);
//Attachments (37xx):
//0x3701: Attachment data <- This is the binary attachment
//0x3703: Attach extension
//0x3704: Attach filename
//0x3707: Attach long filenm
//0x370E: Attach mime tag
//0x3712: Attach ID (?)
// replace attachment with myself file
var myNameIdSourceStorage = memoryStorage.OpenStorage(OutlookStorage.ATTACH_STORAGE_PREFIX + "00000000", IntPtr.Zero, NativeMethods.STGM.READWRITE | NativeMethods.STGM.SHARE_EXCLUSIVE,IntPtr.Zero, 0);
myNameIdSourceStorage.DestroyElement("__substg1.0_37010102");
// Create the property stream again and write in the padded version
var pStream = myNameIdSourceStorage.CreateStream("__substg1.0_37010102",
NativeMethods.STGM.READWRITE | NativeMethods.STGM.SHARE_EXCLUSIVE, 0, 0);
pStream.Write(newFileByte, newFileByte.Length, IntPtr.Zero);
// Open stream from the storage
var mystream = myNameIdSourceStorage.OpenStream("__properties_version1.0", IntPtr.Zero,
NativeMethods.STGM.READWRITE | NativeMethods.STGM.SHARE_EXCLUSIVE, 0);
System.Runtime.InteropServices.ComTypes.STATSTG elementStats;
mystream.Stat(out elementStats, 0);
// Read the stream into a managed byte array
var iStreamContent = new byte[elementStats.cbSize];
mystream.Read(iStreamContent, iStreamContent.Length, IntPtr.Zero);
iStreamContent[0xc0] = (byte)(newFileByte.Length & 0xFF);
iStreamContent[0xc1] = (byte)(newFileByte.Length >> 8);
iStreamContent[0xc2] = (byte)(newFileByte.Length >> 16);
iStreamContent[0xc3] = (byte)(newFileByte.Length >> 24);
mystream.Write(iStreamContent, 0, IntPtr.Zero);
//0x3704: Attach filename
myNameIdSourceStorage.DestroyElement("__substg1.0_3704001F");
pStream = myNameIdSourceStorage.CreateStream("__substg1.0_3704001F",
NativeMethods.STGM.READWRITE | NativeMethods.STGM.SHARE_EXCLUSIVE, 0, 0);
pStream.Write(newProps, 8, IntPtr.Zero);
對不起,但您無法從Web服務器訪問客戶端Outlook實例,因爲Web服務器通常在單獨的計算機上運行。您需要直接訪問Exchange Server。 –
尤爾根有權利。如果你想爲你的客戶創建一個.msg,你不能這樣做。在IISExpress上,它可以工作,因爲它是您的機器和客戶端。如果要在IIS服務器上創建該文件,則必須在其上安裝Outlook,在服務器上創建.msg文件以讀取該文件並將其回覆給客戶端。但出於安全考慮,您將無法在Web服務器上安裝Outlook,並在Web服務器上創建文件來讀取它並將其回覆給您的客戶端顯然不是一個乾淨的解決方案^^。 – D4rkTiger