J
jp2msft
I've created a class to load and save a MailMessage object, but the Save
feature does not work because I have incorrectly coded the Save routine.
Could someone point me in the direction of how to fix this? (I hope this
code is useful to others - I think it is fantastic!)
As always, help is much appreciated!
feature does not work because I have incorrectly coded the Save routine.
Could someone point me in the direction of how to fix this? (I hope this
code is useful to others - I think it is fantastic!)
Code:
public class MailStream : FileStream {
public MailStream(string path, FileMode mode, FileAccess access)
: base(path, mode, access) {
}
public void Load(MailMessage email) {
BindingFlags dwFlag = BindingFlags.Instance | BindingFlags.NonPublic;
Assembly assm = typeof(SmtpClient).Assembly;
using (MemoryStream ms = new MemoryStream()) {
try {
/************************************************************/
Type mailreaderType = assm.GetType("System.Net.Mail.MailReader");
/** ^ It looks like there is no such thing as MailReader ^ **/
ConstructorInfo mailreaderConstructor =
mailreaderType.GetConstructor(dwFlag, null, new Type[] { typeof(Stream) },
null);
object mailReader = mailreaderConstructor.Invoke(new object[] { ms });
MethodInfo sendInfo = typeof(MailMessage).GetMethod("Send", dwFlag);
sendInfo.Invoke(email, dwFlag, null, new object[] { mailReader, true
}, null);
MethodInfo closeInfo = mailReader.GetType().GetMethod("Close",
dwFlag);
ms.WriteTo(this);
closeInfo.Invoke(mailReader, dwFlag, null, new object[] { }, null);
} catch (Exception err) {
throw err;
} finally {
ms.Close();
}
}
}
public void Save(MailMessage email) {
BindingFlags dwFlag = BindingFlags.Instance | BindingFlags.NonPublic;
Assembly assm = typeof(SmtpClient).Assembly;
using (MemoryStream ms = new MemoryStream()) {
try {
Type mailwriterType = assm.GetType("System.Net.Mail.MailWriter");
ConstructorInfo mailwriterConstructor =
mailwriterType.GetConstructor(dwFlag, null, new Type[] { typeof(Stream) },
null);
object mailWriter = mailwriterConstructor.Invoke(new object[] { ms });
MethodInfo sendInfo = typeof(MailMessage).GetMethod("Send", dwFlag);
sendInfo.Invoke(email, dwFlag, null, new object[] { mailWriter, true
}, null);
MethodInfo closeInfo = mailWriter.GetType().GetMethod("Close",
dwFlag);
ms.WriteTo(this);
closeInfo.Invoke(mailWriter, dwFlag, null, new object[] { }, null);
} catch (Exception err) {
throw err;
} finally {
ms.Close();
}
}
}
}
As always, help is much appreciated!