C# 如何将System.Net.MailMessage转换为RawContent
我使用IMAP协议从收件箱获取电子邮件。现在我需要将MailMessage列表的结果转换为RawContent,将其转换为OpenPop.Pop3 Message,但是当我使用 var message=new OpenPop.Mime.message(rawMessage) 如果其内容包含波斯语内容,例如,在标题电子邮件的“From”或“To”部分,如果显示名称为“مسعودبهامی”,则在将其转换为OpenPop消息后,它将转换为“??????” 下面是从MailMessage中提取RawMessage的代码C# 如何将System.Net.MailMessage转换为RawContent,c#,imap,mailmessage,openpop,C#,Imap,Mailmessage,Openpop,我使用IMAP协议从收件箱获取电子邮件。现在我需要将MailMessage列表的结果转换为RawContent,将其转换为OpenPop.Pop3 Message,但是当我使用 var message=new OpenPop.Mime.message(rawMessage) 如果其内容包含波斯语内容,例如,在标题电子邮件的“From”或“To”部分,如果显示名称为“مسعودبهامی”,则在将其转换为OpenPop消息后,它将转换为“??????” 下面是从MailMessage中提取RawM
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;
}
private const BindingFlags Flags=BindingFlags.Instance | BindingFlags.NonPublic;
私有静态只读类型MailWriter=typeof(SmtpClient.Assembly.GetType(“System.Net.Mail.MailWriter”);
私有静态只读构造函数info MailWriterConstructor=MailWriter.GetConstructor(Flags,null,new[]{typeof(Stream)},null);
私有静态只读MethodInfo CloseMethod=MailWriter.GetMethod(“Close”,标志);
私有静态只读MethodInfo SendMethod=typeof(MailMessage).GetMethod(“发送”,标志);
///
///一个确定我们需要的参数数量的小技巧
///需要传递到SaveMethod。
///
私有静态只读bool IsRunningDotNetFourPointFive=SendMethod.GetParameters()。长度==3;
///
///此邮件消息的原始内容为MemoryStream。
///
///打电话的人。
///包含此邮件消息原始内容的MemoryStream。
公共静态MemoryStream RawMessage(此邮件自身)
{
var结果=新的MemoryStream();
var mailWriter=MailWriterConstructor.Invoke(新对象[]{result});
Invoke(self,Flags,null,isRunningDotNetFourPointFive?new[]{mailWriter,true,true}:new[]{mailWriter,true},null);
结果=新的内存流(result.ToArray());
Invoke(mailWriter,Flags,null,新对象[]{},null);
返回结果;
}
尝试更换
结果=新的内存流(result.ToArray())
与:
结果=新的MemoryStream(Encoding.UTF8.GetBytes(result.ToArray().ToString())
希望有帮助。尝试更换
结果=新的内存流(result.ToArray())
与:
结果=新的MemoryStream(Encoding.UTF8.GetBytes(result.ToArray().ToString())
希望有帮助。谢谢,但此网站可能会导致“内存流不可扩展”异常。谢谢,但此网站可能会导致“内存流不可扩展”异常