Exchange Web Services Managed API 1.1 を使用して Exchange サーバー 2010 に接続し、受信した新しい電子メールを見つけます。ここで、.msg ファイルのコピーをディスク上のフォルダーに保存したいと考えています。
有料のサードパーティを使用して統合したくありません。
どんな助けでも大歓迎です。
Exchange Web Services Managed API 1.1 を使用して Exchange サーバー 2010 に接続し、受信した新しい電子メールを見つけます。ここで、.msg ファイルのコピーをディスク上のフォルダーに保存したいと考えています。
有料のサードパーティを使用して統合したくありません。
どんな助けでも大歓迎です。
代わりに形式に保存することに満足している場合は.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);
}
EWS を使用した MSG ファイルのネイティブ サポートはありません。厳密には Outlook 形式です。
MSG 仕様はhttp://msdn.microsoft.com/en-us/library/cc463912%28EXCHG.80%29.aspxで公開されています。理解するのは少し複雑ですが、実行可能です。メッセージのすべてのプロパティを取得してから、OLE 構造化ファイル形式にシリアル化する必要があります。それは簡単な作業ではありません。
最終的には、おそらくサード パーティ製のライブラリを使用する方がよいでしょう。
メッセージの MIME コンテンツに簡単にアクセスし、message.MimeContent
メッセージを EML ファイルとして保存できます。Outlook の最新 (2013 および 2016) バージョンでは、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 形式に変換する必要がある場合は、いくつかのオプションがあります。
MSG ファイル形式は文書化されています - これは OLE ストア (IStorage) ファイルです。https://msdn.microsoft.com/en-us/library/cc463912(v=exchg.80).aspxを参照してください。
Independentsoft のhttp://www.independentsoft.de/msg/index.htmlなど、サード パーティの MSG ファイル ラッパーを使用します。Outlook が期待するすべてのプロパティを設定するのは難しい場合があります。
Redemptionを使用して、EML ファイルを MSG に直接変換します。
set Session = CreateObject("Redemption.RDOSession") set Msg = Session.CreateMessageFromMsgFile("c:\test.msg") Msg.Import("c:\test.eml", 1024) Msg.Save
MIME はすべての MAPI 固有のプロパティを保持しないことに注意してください。ExportItems EWS 操作で使用される Fast Transfer Stream (FTS) 形式を使用できます(MSG 形式と同様に、ほとんどの MAPI プロパティが保持されます)。その後、Redemption ( RDOSession . CreateMessageFromMsgFile
/ RDOMail. Import(..., olFTS)
/ RDOMail.Save
)を使用して、FTS データを (忠実度を損なうことなく) MSG 形式に変換できます。
RDOSession session = new RDOSession(); RDOMail msg = session.CreateMessageFromMsgFile(@"c:\temp\test.msg"); msg.Import(@"c:\temp\test.fts", rdoSaveAsType.olFTS); msg.Save();
この提案は@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);
}
eml 形式がオプションで、php が言語の場合、ファイルに保存する前に Mimencontent で base64_decode を使用します。
https://github.com/Heartspring/Exchange-Web-Services-for-PHPまたはhttps://github.com/hatsuseno/Exchange-Web-Services-for-PHPを使用している場合は、追加する必要があります
$newmessage->mc = $messageobj->MimeContent->_;
245 行目または 247 行目。
Outlook の EntryID から VSTO (16 進数) 経由で EwsID に移行する場合は、こちらを参照する必要があります: http://bernhardelbl.wordpress.com/2013/04/15/converting-entryid-to-ewsid-using-exchange-web -services-ews/
助かりました。「データが壊れています」というメッセージが表示され続けました。メッセージ。
EWS API と C# を使用して、すべての添付ファイルをダウンロードできます。以下に例を示します。
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++;
}
}
}
}
上記のコードは、すべての添付ファイルをバイトに変換します。バイトを取得したら、バイトを必要な形式に変換できます。バイト をファイルに変換してディスクに保存するには、以下のリンクに従って ください。 -file.html