如何将Outlook MailItem对象转换为byte [] [英] How can I convert an Outlook MailItem object to a byte[]
问题描述
有一些关于这个Stackoverflow的问题,但没有一个真的提供了一个解决方案。
场景 - 我在VS2013中创建一个Outlook AddIn。用户选择电子邮件,点击我的AddIn按钮,并将电子邮件发送到一个Web服务,以存储在数据库中并链接到客户端。任何人都可以打开电子邮件查看它。
目前我正在使用 MailItem.SaveAs(filePath)
函数,然后使用 File.ReadAllBytes(filePath)
创建一个可发送到webservice的字节数组。
一旦我创建字节[],我删除该文件:
for(int x = 0 ; x< Emails.Count; x ++)
{
// TODO:RMc - 有比存储到磁盘更好的方法吗?无法将MailItem直接转换为流...!
Guid g = Guid.NewGuid();
string filePath =c:\\temp\\+ g.ToString()+.msg;
Emails.ElementAt(x).SaveAs(filePath);
byte [] by = File.ReadAllBytes(filePath);
File.Delete(filePath); //现在不再需要我们有字节数组
//这是我创建一个对象列表,并将所有必需的数据发送到Web服务
}
将文件写入磁盘很慢 - 它创建一个*。 msg文件,如果没有人想要查看它可能永远不会被使用。所以我希望能够将MailItem对象直接保存到一个字节数组,然后我可以将其保存到数据库中,并且只有在用户需要时才创建* .msg文件。
MailItem对象看起来是动态的,所以我认为这是问题。
任何人都可以提供解决方案或替代方法来实现什么我已经描述过了吗?
Outlook对象模型和扩展MAPI中的消息不可流水。您可以以特定格式保存邮件,但不会是完整的副本。 MSG格式将是您最接近的。
使用 MailItem.SaveAs(...,olMsg)
将消息保存为MSG文件。 p>
There are a few Stackoverflow questions about this but none of them really offer a solution.
The Scenario - I'm creating an Outlook AddIn in VS2013. The user selects emails, hits my AddIn button and the emails are sent to a webservice to be stored in a database and linked to a client. Anyone, in any location will be able to open the email to view it.
Currently I am using the MailItem.SaveAs(filePath)
function, then using File.ReadAllBytes(filePath)
to create a byte array that can be sent to the webservice.
I delete the file as soon as I create the byte[]:
for (int x = 0; x < Emails.Count; x++)
{
//TODO: RMc - is there a better way than saving to disk? unable to convert MailItem directly to stream...!
Guid g = Guid.NewGuid();
string filePath = "c:\\temp\\" + g.ToString() + ".msg";
Emails.ElementAt(x).SaveAs(filePath);
byte[] by = File.ReadAllBytes(filePath);
File.Delete(filePath);//no longer needed now we have byte array
//this is where I create a list of objects with all required data to send to web service
}
Writing the file to disk is slow - it creates a *.msg file that may never actually get used if no one wants to view it. So I would like to be able to save the MailItem object to a byte array directly - and then I could save that to the database and only create the *.msg file if a user requires it.
The MailItem object appears to by dynamic, and so I think this is the problem.
Can anyone offer a solution or an alternative way of achieving what I have described?
Messages in the Outlook Object Model and Extended MAPI are not streamable. You can save a message in a particular format, but it will not be an exact copy. MSG format will be the closest you can get.
Use MailItem.SaveAs(..., olMsg)
to save a message as an MSG file.
这篇关于如何将Outlook MailItem对象转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!