Creating a MailStream : StreamReader


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!)

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!
 
Ad

Advertisements

A

Arne Vajhøj

jp2msft said:
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!)
Type mailreaderType = assm.GetType("System.Net.Mail.MailReader");
/** ^ It looks like there is no such thing as MailReader ^ **/

http://msdn.microsoft.com/en-us/library/system.net.mail.aspx

and Reflector agrees !

Arne
 
J

jp2msft

MSDN is not helpful in this case. See the Save code snippet that works great:
Type mailwriterType = assm.GetType("System.Net.Mail.MailWriter");


MailWriter is not documented with MSDN either, but it works great.

I'm glad you know how to use Reflector (I don't), and I'd appreciate it if
someone would let me know how to access a "MailRead" feature. Obviously it
exists, otherwise, System.Net.Mail would not be able to read a mail message.
 
A

Arne Vajhøj

jp2msft said:
MSDN is not helpful in this case. See the Save code snippet that works great:



MailWriter is not documented with MSDN either, but it works great.

I'm glad you know how to use Reflector (I don't), and I'd appreciate it if
someone would let me know how to access a "MailRead" feature. Obviously it
exists, otherwise, System.Net.Mail would not be able to read a mail message.

As far as I know, then System.Net.Mail are not able to read
a mail message !

You need to find a POP3 or IMAP4 lib for .NET somewhere.

Arne
 
F

Family Tree Mike

jp2msft said:
MSDN is not helpful in this case. See the Save code snippet that works great:



MailWriter is not documented with MSDN either, but it works great.

I'm glad you know how to use Reflector (I don't), and I'd appreciate it if
someone would let me know how to access a "MailRead" feature. Obviously it
exists, otherwise, System.Net.Mail would not be able to read a mail message.

This vertical shows a way for using MailWriter that indicates what is
reads its output. Perhaps it helps.
http://www.codeproject.com/KB/IP/smtpclientext.aspx

I liked the intro:
This article is going to use some simple techniques to analyse an
existing .NET framework class and to extend it in a way the original
developers did not intend.
 
J

jp2msft

I guess that answers the question, though it doesn't seem very helpful.

A search for POP3, IMAP4, or Mail Reader pulls up gobs of irrelevant other
pages.

So, do you know of any way to "create" a MailMessage object from a chunk of
memory that is a saved MailMessage? I tried casting a byte array to a
MailMessage, but C# could not do that.
 
Ad

Advertisements

A

Arne Vajhøj

jp2msft said:
I guess that answers the question, though it doesn't seem very helpful.

A search for POP3, IMAP4, or Mail Reader pulls up gobs of irrelevant other
pages.

If I google for:
C# POP3
then I get lot of relevant hits.
So, do you know of any way to "create" a MailMessage object from a chunk of
memory that is a saved MailMessage? I tried casting a byte array to a
MailMessage, but C# could not do that.

I think you will need to parse the content and construct the message
from the parts.

Arne
 
J

jp2msft

Hi Mike,

That article was actually where I found the hidden MailWriter and how to
access it.

So, I can save a MailMessage on the PC, but I can find no way to write
anything that can say something like:

MailMessage email = (MailMessage)open(filename);

In a nutshell, that's my goal now that I know how to save a message. Any
idea how I could go about retrieving a saved message now?
 
F

Family Tree Mike

jp2msft said:
Hi Mike,

That article was actually where I found the hidden MailWriter and how to
access it.

So, I can save a MailMessage on the PC, but I can find no way to write
anything that can say something like:

MailMessage email = (MailMessage)open(filename);

In a nutshell, that's my goal now that I know how to save a message. Any
idea how I could go about retrieving a saved message now?

I took this sentance:

To actually generate the email, it then calls a Send() method on the
MailMessage object, passing the newly created MailWriter object.

I interpreted that to mean that it would fill a null message when the
send method was called. Sorry if my interpretation is wrong.
 
Ad

Advertisements

A

Arne Vajhøj

jp2msft said:
That article was actually where I found the hidden MailWriter and how to
access it.

So, I can save a MailMessage on the PC, but I can find no way to write
anything that can say something like:

MailMessage email = (MailMessage)open(filename);

In a nutshell, that's my goal now that I know how to save a message. Any
idea how I could go about retrieving a saved message now?

If the mail is pure RFC 821/822 (or their replacements), then
parsing should be rather simple.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top