我正在使用Exchange Web服務託管API 1.1連接到Exchange Server 2010,然後找出收到的新電子郵件。現在我想將.msg文件的副本保存到磁盤上的文件夾中。使用EWS API將郵件保存到msg文件
我不想使用任何有償的第三方進行整合。
任何幫助將不勝感激。
我正在使用Exchange Web服務託管API 1.1連接到Exchange Server 2010,然後找出收到的新電子郵件。現在我想將.msg文件的副本保存到磁盤上的文件夾中。使用EWS API將郵件保存到msg文件
我不想使用任何有償的第三方進行整合。
任何幫助將不勝感激。
對於使用EWS的MSG文件沒有本機支持。這完全是一種Outlook格式。
MSG規範發佈在http://msdn.microsoft.com/en-us/library/cc463912%28EXCHG.80%29.aspx。理解有點複雜,但可以實現。您需要拉下消息的所有屬性,然後將其序列化爲OLE結構化文件格式。這不是一件容易的事。
最後,你可能會更適合使用第三方庫,否則這可能是一項艱鉅的任務。
如果您樂意保存爲.eml
格式,那麼只需使用EWS並且無需第三方庫即可輕鬆完成。 .eml
文件將包含所有相同的信息,並且可以通過Outlook以.msg(以及其他程序)相同的方式打開。
message.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mc = message.MimeContent;
FileStream fs = new FileStream("c:\test.eml", FileMode.Create);
fs.Write(mc.Content, 0, mc.Content.Length);
fs.Close();
盪滌代碼:
message.Load(new PropertySet(ItemSchema.MimeContent));
var mimeContent = message.MimeContent;
using (var fileStream = new FileStream(@"C:\Test.eml", FileMode.Create))
{
fileStream.Write(mimeContent.Content, 0, mimeContent.Content.Length);
}
我知道這已經過去了,但謝謝你的回答。爲我節省了很多時間。 – Fox 2012-03-02 12:41:56
同意。這非常有幫助!我只是改變了一點,使用using語句: 使用(FileStream fileStream = File.Open(@「C:\ message.eml」,FileMode.Create,FileAccess.Write)) { message.Load(new PropertySet (ItemSchema.MimeContent)); MimeContent mc = message.MimeContent; fileStream.Write(mc.Content,0,mc.Content.Length); } – mack 2013-05-23 14:54:59
我通過包含'using'語句來清理代碼,並且還用字符串文字('@「C:\ Test.eml」')糾正了一個錯誤。 – 2014-02-11 02:54:56
如果EML格式是一個選項,PHP是在Mimencontent語言使用BASE64_DECODE之前保存的文件。
如果使用https://github.com/Heartspring/Exchange-Web-Services-for-PHP或https://github.com/hatsuseno/Exchange-Web-Services-for-PHP需要上線245或247
您可以下載使用EWS API和C#中的所有附件添加
$newmessage->mc = $messageobj->MimeContent->_;
。下面是給出的實例:
byte[][] btAttachments = new byte[3][]; //To store 3 attachment
if (item.HasAttachments)
{
EmailMessage message = EmailMessage.Bind(objService, new ItemId(item.Id.UniqueId.ToString()), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
noOfAttachment = message.Attachments.Count;
// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the file attachment into memory and print out its file name.
fileAttachment.Load();
//Get the Attachment as bytes
if (i < 3)
{
btAttachments[i] = fileAttachment.Content;
i++;
}
}
// Attachment is an item attachment.
else
{
// Load attachment into memory and write out the subject.
ItemAttachment itemAttachment = attachment as ItemAttachment;
itemAttachment.Load(new PropertySet(EmailMessageSchema.MimeContent));
MimeContent mc = itemAttachment.Item.MimeContent;
if (i < 3)
{
btAttachments[i] = mc.Content;
i++;
}
}
}
}
以上代碼轉換所有的附接成字節。一旦你有字節,你可以將字節轉換爲你需要的格式。 轉換字節到文件,並保存在磁盤按照下面的鏈接: Write bytes to file http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Save-byte-array-to-file.html
這個建議被張貼由@mack評論,但我認爲它值得自己的位置作爲一個答案,如果沒有除了格式和答案與評論的可讀性之外的其他原因。
using (FileStream fileStream =
File.Open(@"C:\message.eml", FileMode.Create, FileAccess.Write))
{
message.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mc = message.MimeContent;
fileStream.Write(mc.Content, 0, mc.Content.Length);
}
這是我如何解決從EWS以.eml格式通過VBS代碼下載郵件中的問題
' This is the function that retrieves the message:
function CreaMailMsg(ItemId,ChangeKey)
Dim MailMsg
Dim GetItemSOAP,GetItemResponse,Content
LogFile.WriteLine (Now() & "-" & ":CreaMailMsg:ID:" & ItemId)
GetItemSOAP=ReadTemplate("GetItemMsg.xml")
GetItemSOAP=Replace(GetItemSOAP, "<!--ITEMID-->", ItemId)
GetItemSOAP=Replace(GetItemSOAP, "<!--ITEMCHANGEKEY-->", ChangeKey)
LogFile.WriteLine (Now() & ":GetItemSOAP:" & GetItemSOAP)
set GetItemResponse=SendSOAP(GetItemSOAP,TARGETURL,"",USERNAME,PASSWORD)
' Check we got a Success response
if not IsResponseSuccess(GetItemResponse, "m:GetItemResponseMessage","ResponseClass") then
LogFile.WriteLine (Now() & "-" & ":ERRORE:Fallita GetItemMsg:" & GetItemResponse.xml)
Chiusura 1
end if
' LogFile.WriteLine (Now() & "-" & ":DEBUG:riuscita GetItemMsg:" & GetItemResponse.xml)
Content = GetItemResponse.documentElement.getElementsByTagName("t:MimeContent").Item(0).Text
' LogFile.WriteLine (Now() & ":Contenuto MIME" & Content)
CreaMailMsg = WriteAttach2File(Content,"OriginaryMsg.eml")
' MailMsg.close
CreaMailMsg = true
end function
'###########################################################################
' These are the functions the save the message in .eml format
'###########################################################################
function WriteAttach2File(Content,nomeAttach)
Dim oNode,oXML,Base64Decode
' Read the contents Base64 encoded and Write a file
set oXML=CreateObject("MSXML2.DOMDocument")
set oNode=oXML.CreateElement("base64")
oNode.DataType="bin.base64"
oNode.Text = Content
Base64Decode = Stream_Binary2String(oNode.nodeTypedValue,nomeAttach)
Set oNode = Nothing
Set oXML = Nothing
end function
'###########################################################################
function Stream_Binary2String(binary,nomeAttach)
Const adTypeText = 2
Const adTypeBinary = 1
Dim BinaryStream
Set BinaryStream=CreateObject("ADODB.Stream")
BinaryStream.Type=adTypeBinary' Binary
BinaryStream.Open
BinaryStream.Write binary
BinaryStream.Position=0
BinaryStream.Type=adTypeText
BinaryStream.CharSet = "us-ascii"
Stream_Binary2String=BinaryStream.ReadText
'msgbox Stream_Binary2String
BinaryStream.SaveToFile ShareName & "\" & nomeAttach,2
Set BinaryStream=Nothing
end function
如果從Outlook的的EntryID通過VSTO(十六進制)要EwsID,你需要看看這裏:http://bernhardelbl.wordpress.com/2013/04/15/converting-entryid-to-ewsid-using-exchange-web-services-ews/
救了我。我不斷收到「數據已損壞」。信息。
您可以通過message.MimeContent輕鬆訪問消息的MIME內容並將消息另存爲EML文件。最新的(2013和2016)版本的Outlook將能夠直接打開EML文件。
message.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mimcon = message.MimeContent;
FileStream fStream = new FileStream("c:\test.eml", FileMode.Create);
fStream.Write(mimcon.Content, 0, mimcon.Content.Length);
fStream.Close();
如果你仍然需要轉換爲MSG格式,你有幾種選擇:
1)MSG文件格式記錄 - 這是一個OLE店(的IStorage)文件。請參閱https://msdn.microsoft.com/en-us/library/cc463912(v=exchg.80).aspx
2)使用第三方MSG文件包裝器,例如Independentsoft的包裝器:http://www.independentsoft.de/msg/index.html。設置Outlook所需的所有屬性都可能具有挑戰性。
3)轉換EML文件味精直接使用Redemption:
set Session = CreateObject("Redemption.RDOSession")
set Msg = Session.CreateMessageFromMsgFile("c:\test.msg")
Msg.Import("c:\test.eml", 1024)
Msg.Save
保存爲***。eml ***非常簡單,但保存爲***。msg ***需要**第三方**(_NOT free_) – Kiquenet 2017-06-10 07:49:00
您仍然可以直接使用二進制級別的MSG文件,但問題是這樣做是否會爲您節省時間或金錢 - 可能不會。 – 2017-06-11 18:12:13
做你試圖保存到*** EML *** ***或***味精文件。? – Kiquenet 2017-06-10 07:47:14