我正在建立一個應用程序,我有義務創建一個MailMessage(System.Net.mail.MailMessage)並將它保存在磁盤上,因爲.msg擴展名不是.eml保存System.Net.mail.MailMessage作爲.msg文件
下面是我使用保存作爲MAILMESSAGE .msg文件的方法:
public static void Save(MailMessage Message, string FileName)
{
Assembly assembly = typeof(SmtpClient).Assembly;
Type _mailWriterType =
assembly.GetType("System.Net.Mail.MailWriter");
using (FileStream _fileStream =
new FileStream(FileName, FileMode.Create))
{
// Get reflection info for MailWriter contructor
ConstructorInfo _mailWriterContructor =
_mailWriterType.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { typeof(Stream) },
null);
// Construct MailWriter object with our FileStream
object _mailWriter =
_mailWriterContructor.Invoke(new object[] { _fileStream });
// Get reflection info for Send() method on MailMessage
MethodInfo _sendMethod =
typeof(MailMessage).GetMethod(
"Send",
BindingFlags.Instance | BindingFlags.NonPublic);
// Call method passing in MailWriter
_sendMethod.Invoke(
Message,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { _mailWriter, true },
null);
// Finally get reflection info for Close() method on our MailWriter
MethodInfo _closeMethod =
_mailWriter.GetType().GetMethod(
"Close",
BindingFlags.Instance | BindingFlags.NonPublic);
// Call close method
_closeMethod.Invoke(
_mailWriter,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { },
null);
}
}
但保存的MSG文件無法打開,並在下面的錯誤: 「無法打開文件XYZ .msg文件文件可能不存在,您可能沒有權限打開它或者它可能被另一個程序打開....「
我的問題是:如何將System.Net.mail.MailMessage保存爲msg文件?
是的,這是真的,但MailMessage保存爲.eml文件,我希望它被保存爲.msg文件 – Gaby
爲什麼downvoting這?這不是「答案」,但這是一個很好的建議...... –
加比,你有答案嗎? – zipi