如何将 System.Net.MailMessage 转换为 RawContent



>我使用 IMAP 协议从收件箱获取电子邮件。现在我需要将作为邮件列表的结果转换为 RawContent,以将其转换为 OpenPop.Pop3 消息,但是当我使用

var message = new OpenPop.Mime.Message(rawMessage);

如果

它的内容包含波斯语内容,例如在标题电子邮件的"发件人"或"收件人"部分中,如果显示名称为"مسعود بهرامی",则将其转换为 OpenPop 消息后,它将转换为"??????????????"

这是从邮件中提取原始消息的代码

private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.NonPublic;
    private static readonly Type MailWriter = typeof(SmtpClient).Assembly.GetType("System.Net.Mail.MailWriter");
    private static readonly ConstructorInfo MailWriterConstructor = MailWriter.GetConstructor(Flags, null, new[] { typeof(Stream) }, null);
    private static readonly MethodInfo CloseMethod = MailWriter.GetMethod("Close", Flags);
    private static readonly MethodInfo SendMethod = typeof(MailMessage).GetMethod("Send", Flags);
    /// <summary>
    /// A little hack to determine the number of parameters that we
    /// need to pass to the SaveMethod.
    /// </summary>
    private static readonly bool IsRunningInDotNetFourPointFive = SendMethod.GetParameters().Length == 3;
    /// <summary>
    /// The raw contents of this MailMessage as a MemoryStream.
    /// </summary>
    /// <param name="self">The caller.</param>
    /// <returns>A MemoryStream with the raw contents of this MailMessage.</returns>
    public static MemoryStream RawMessage(this MailMessage self)
    {
        var result = new MemoryStream();
        var mailWriter = MailWriterConstructor.Invoke(new object[] { result });
        SendMethod.Invoke(self, Flags, null, IsRunningInDotNetFourPointFive ? new[] { mailWriter, true, true } : new[] { mailWriter, true }, null);
        result = new MemoryStream(result.ToArray());
        CloseMethod.Invoke(mailWriter, Flags, null, new object[] { }, null);
        return result;
    }

尝试替换

result

= new MemoryStream(result.ToArray());

跟:

result

= new MemoryStream(Encoding.UTF8.GetBytes(result.ToArray()。ToString()));

希望对您有所帮助。

相关内容

  • 没有找到相关文章

最新更新