Openpop.NET 检索附件到特定文件夹

发布于 2024-11-28 04:45:22 字数 609 浏览 1 评论 0原文

我正在尝试使用下面的代码检索电子邮件附件并将其保存到文件系统中的特定目录。

Dim objMail As Message = New Message(Encoding.ASCII.GetBytes(strMessage))

For Each att In objMail.FindAllAttachments()
    Dim Stream as FileStream = New FileStream("D:\XX\" & att.FileName.ToString(), 
        FileMode.Create)
    Dim BinaryStream As New BinaryWriter(Stream)
    BinaryStream.Write(BitConverter.ToString(att.Body))
    BinaryStream.Close()
Next

我还尝试过 att.GetBodyasText()

使用此代码我可以将附件文件保存在所需的文件夹中。但是在打开文件时,我收到错误L

文件格式不正确或解码不正确。

我是 MIME 编码/解码新手。

I am trying to retrieve an email attachment and save it to specific directory in filesystem using the code below.

Dim objMail As Message = New Message(Encoding.ASCII.GetBytes(strMessage))

For Each att In objMail.FindAllAttachments()
    Dim Stream as FileStream = New FileStream("D:\XX\" & att.FileName.ToString(), 
        FileMode.Create)
    Dim BinaryStream As New BinaryWriter(Stream)
    BinaryStream.Write(BitConverter.ToString(att.Body))
    BinaryStream.Close()
Next

I have also tried att.GetBodyasText()

Using this code I am able to save attachment file in desired folder. But while opening a file, I am getting errorL

File is not in proper Format or not Decoded Properly.

I am new to MIME encoding/decoding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

神经大条 2024-12-05 04:45:22

我是 OpenPop.NET 的开发人员。

您用来实例化 Message 类的代码存在多个问题:

  1. strMessage 内容来自哪里?
  2. 你怎么知道它只用 ASCII 编码?

这是两个可能会产生重大影响的主要问题。
您不应该使用字符串来包含消息,而应该使用原始字节!

例如(在 C# 中):

byte[] byteMessage = someFileStream.ReadToEnd();
Message message = new Message(byteMessage);

这样,您就不会因在字节上使用错误的编码而破坏消息。通常,电子邮件将包含一个标头,告诉您如何将字节解码为字符串,这就是 OpenPop Message 类将为您做的事情。

现在让我解释一下附件。附件通常是原始字节,例如 PNG 图片是 PNG 图片阅读器可以理解的一些字节。为了让 PNG 图片阅读器理解图片,必须将附件原始字节保存到文件中。您可以使用 att.Body 获取原始字节。

还有一些附件的原始字节没有意义 - 例如,以 BASE64 编码的文本附件对于文本阅读器程序来说不是很有用,并且此类附件在保存之前必须转换为文本。您可以使用 att.GetBodyAsText() 获取文本。

您正在做的是获取附件的原始字节,然后使用 BitConverter 将其转换为十六进制数字 - 我无法理解它的任何含义。相反,您应该更改您的:

BinaryStream.Write(BitConverter.ToString(att.Body)) 

以防

BinaryStream.Write(att.Body)

您的附件是图片或一些更复杂的文件。

我希望这可以帮助解决您的问题。

I am a developer of OpenPop.NET.

There are multiple issues with the code you are using to instantiate the Message class:

  1. Where is the strMessage contents comming from?
  2. How do you know it is encoded in only ASCII?

These are two major issues that will likely make a big difference.
You should NOT be using a string to contain the message, instead you should be using raw bytes!

For example (in C#):

byte[] byteMessage = someFileStream.ReadToEnd();
Message message = new Message(byteMessage);

In this way, you will not destroy the message by using a wrong encoding on the bytes. Typically the email will include a header which tells how to decode to bytes to a string, which is what the OpenPop Message class will do for you.

Now let me explain attachments. Attachments are typically raw bytes, for example a PNG picture is some bytes that a PNG picture reader will understand. For the PNG picture reader to understand the picture, the attachments raw bytes must be saved to a file. You can get the raw bytes using att.Body.

There are also attachments where the raw bytes does not make sense - for example a text attachment encoded in BASE64 is not very useful for a text reader program, and such an attachment must be converted to text before saved. You can get the text using att.GetBodyAsText().

What you are doing is taking the raw bytes for the attachment and then using a BitConverter to convert it into hexadecimal numbers - which I cannot make any meaning of. Instead, you should change your:

BinaryStream.Write(BitConverter.ToString(att.Body)) 

to

BinaryStream.Write(att.Body)

in case your attachment is a picture or some more complex file.

I hope this can help with your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文