C# 文件附件内容到字符串

发布于 2024-11-09 15:40:01 字数 1390 浏览 3 评论 0原文

我正在尝试从电子邮件的附件中获取内容。我能够阅读电子邮件及其所有属性。附件是文本文件 (.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-11-16 15:40:01

那是因为它是一个字节数组,您必须使用 StreamReader 来获取它

var stream = new System.IO.MemoryStream(fileAttachment.Content);
var reader = new System.IO.StreamReader(stream, UTF8Encoding.UTF8);
var text = reader.ReadToEnd();

That's because it is an array of bytes you'll have to use a StreamReader to get it

var stream = new System.IO.MemoryStream(fileAttachment.Content);
var reader = new System.IO.StreamReader(stream, UTF8Encoding.UTF8);
var text = reader.ReadToEnd();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文