0

どのように改宗できますか

System.Net.MailMessage

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;
    }
4

1 に答える 1