将 System.Net.MailMessage 转换为 OpenPop.Mime.Message



我可以转换吗

系统.网络.邮件消息

OpenPop.Mime.Message

以创建新的 OpenPop.Mime.Message 消息。首先,我使用以下代码提取MailMessage的RawMessage,然后将其发送到OpenPop.Mime.Message的构造函数以创建消息的实例,但是如果电子邮件包含波斯语内容,例如发件人的显示名称是波斯语("مسعودبهرامی"),转换后发件人更改为"??????????"`

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 byte[] 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);
        return null;
        //result = new MemoryStream(result);
        //result = new MemoryStream(Encoding.UTF8.GetBytes(result.ToArray().ToString()));
        CloseMethod.Invoke(mailWriter, Flags, null, new object[] { }, null);
        //return result;
    }

看起来 System.Net.MailMessage 不会公开任何会为您执行 byte[] 格式设置的方法,因此您必须创建一个 ToByteArray 方法来构造一个字节数组,您可以将其传递给 OpenPop.Mime.Message 的构造函数以创建消息对象。您可以从以下系统推断出所需数组的格式:

将 Gmail API 邮件转换为 OpenPop Mime 邮件

这并不能完全解决您的问题,但我希望它能为您提供足够的工作。

相关内容

  • 没有找到相关文章

最新更新