C# 文件附件内容到字符串
我正在尝试从电子邮件的附件中获取内容。我能够阅读电子邮件及其所有属性。附件是文本文件 (.txt)。我如何获取附件的实际文本内容?下面是我的代码的一部分,显示了我如何抓取每封电子邮件,然后获取其属性并将它们存储在 findResults 列表中。像 fileAttachment.Name 这样的东西会给我附件的名称。 fileAttachment.content.tostring() 只会显示 system.Byte[]。
FindItemsResults<Item> findResults = service.FindItems(fid1, new ItemView(int.MaxValue));
service.LoadPropertiesForItems(from Item item in findResults select item, PropertySet.FirstClassProperties);
foreach (Item item in findResults)
{
String body = "";
#region attachments
if (ReadAttachments == "1")
{
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
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();
String attachbody = fileAttachment.Content.ToString();
if (attachbody.Length > 8000)
{
attachbody = attachbody.Substring(0, 8000);
}
Console.writeline(attachbody);
#endregion
}
}
}
#endregion
}
I am trying to get the content from an attachment on an email. I am able to read the email and all of its attributes. The attachment is a text file (.txt). How would i grab the actually text content of the attachment? Below is a section of my code showing how i grab each email and then get its attributes and store them in the findResults list. Something like fileAttachment.Name would give me the name of the attachment. fileAttachment.content.tostring() will only show me system.Byte[].
FindItemsResults<Item> findResults = service.FindItems(fid1, new ItemView(int.MaxValue));
service.LoadPropertiesForItems(from Item item in findResults select item, PropertySet.FirstClassProperties);
foreach (Item item in findResults)
{
String body = "";
#region attachments
if (ReadAttachments == "1")
{
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
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();
String attachbody = fileAttachment.Content.ToString();
if (attachbody.Length > 8000)
{
attachbody = attachbody.Substring(0, 8000);
}
Console.writeline(attachbody);
#endregion
}
}
}
#endregion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为它是一个字节数组,您必须使用 StreamReader 来获取它
That's because it is an array of bytes you'll have to use a StreamReader to get it