感谢所有,最后我做到这一点如下: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