Converting an unserializeable object to a byte array

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

Can anyone please tell me how to convert an unserializeable object say, a
System.Web.Mail.MailMessage object, to a byte array and then convert the
byte array to a Base64 string?

Any assistance gratefully received.

Best regards,

Mark Rae
 
Mark said:
Hi,

Can anyone please tell me how to convert an unserializeable object
say, a System.Web.Mail.MailMessage object, to a byte array and then
convert the byte array to a Base64 string?

Any assistance gratefully received.

Best regards,

Mark Rae

Mark,

I would manually build a byte array from the properties and
System.Text.Encoding.ASCII.GetBytes() (or unicode), then you can convert it
to Base64 with Convert.ToBase64String().

- Pete
 
Pete,

Thanks for the reply.
I would manually build a byte array from the properties and
System.Text.Encoding.ASCII.GetBytes() (or unicode)

I'm obviously missing something here...are you suggesting something like the
following:

MailMessage objMailMessage = new MailMessage();
byte[] = System.Text.ASCII.Encoding.GetBytes(objMailMessage);

Because, as far as I can see, System.Text.Encoding.ASCII.GetBytes() has 5
overloads, all of which take a string as their first (or only) parameter.
I'm trying to find a way to do this on a MailMessage object, which is
unserializeable.

Apologies if this isn't what you meant but, for the moment, I can't get your
suggestion to work.

Best,

Mark
 
Mark said:
Pete,

Thanks for the reply.
I would manually build a byte array from the properties and
System.Text.Encoding.ASCII.GetBytes() (or unicode)

I'm obviously missing something here...are you suggesting something
like the following:

MailMessage objMailMessage = new MailMessage();
byte[] = System.Text.ASCII.Encoding.GetBytes(objMailMessage);

Because, as far as I can see, System.Text.Encoding.ASCII.GetBytes()
has 5 overloads, all of which take a string as their first (or only)
parameter. I'm trying to find a way to do this on a MailMessage
object, which is unserializeable.

Apologies if this isn't what you meant but, for the moment, I can't
get your suggestion to work.

Best,

Mark

Something like this (I don't remember the properties MailMessage has):
byte[] sender = System.Text.ASCII.Encoding.GetBytes(objMailMessage.Sender);
byte[] recipient =
System.Text.ASCII.Encoding.GetBytes(objMailMessage.Recipient);
byte[] body = System.Text.ASCII.Encoding.GetBytes(objMailMessage.Body);

Then combine them (psuedocode):
MemoryStream all = new MemoryStream()
all.Write((short)sender.Length);
all.Write(sender);
all.Write((short)recipient.Length);
all.Write(recipient);
all.Write((short)body.Length);
all.Write(body);

The intended result is like this:
0000 sender len, 2 bytes
0002 sender, ascii
???? recip, len, 2 bytes
???? recip, ascii
???? body len, 2 bytes
???? body, ascii

Then:
string base64message = Convert.ToBase64String(all.GetBuffer());


- Pete
 
Pete,

Thanks for that, but if I did this I may as well just pass the individual
properties individually, if you see what I mean :-) because I'd have to
unstitch it all at the other end in order to build up the MailMessage object
again. Looks like I'll just have to tell my client that this isn't possible.

Mark
 
Mark said:
Pete,

Thanks for that, but if I did this I may as well just pass the
individual properties individually, if you see what I mean :-)
because I'd have to unstitch it all at the other end in order to
build up the MailMessage object again. Looks like I'll just have to
tell my client that this isn't possible.

Could you create your own, serializable version of MailMessage, and provide
conversion functions to and from the .net MailMessage?+
Mark
[snip]
 
Pete,
Could you create your own, serializable version of MailMessage, and provide
conversion functions to and from the .net MailMessage?+

Hmm - that's something I could look at...

The main reason for trying to do this in the first place is to (try to)
avoid the "roll-your-own" approach so that I don't have to worry about
things like attachments etc.

Basically, we have a web service which has two WebMethods: SendMail and
SendMailEx. They do essentially the same thing apart from the fact that
SendMailEx supports attachments via the XML DIME extensions. It all works,
and everyone's happy etc. However, my client has asked me to look into the
possibility of an "enclosed" .NET solution whereby remote sites would build
up a MailMessage object and pass only that to a new WebMethod called
SendMailDotNet. I have said that it probably isn't possible natively because
the MailMessage object itself isn't serializeable, but that it *MAY* be
possible by trying to "wrap" it in something which makes it serializeable so
that it can be passed across the Internet to a Web Service. My client said
that if it involved splitting the MailMessage out into its constituent
parts, then not to bother because we may as well just continue with the
SendMailEx WebMethod. I found some code on the web which took a structure
and converted it into a byte array, but this didn't work for the MailMessage
object either.

I'm struggling to work out why this seems to be so difficult...isn't a
MailMessage object just an object in memory? Why does it seem to be so
difficult to get an array of its bytes? This ought to be the easiest thing
in the world, something like (again, apologies for the C#)...

MailMessage objMailMessage = new MailMessage();
MemoryStream objMS = new MemoryStream();
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMS, objMailMessage);
byte[] abytMailMessage = objMS.GetBuffer();

Best,

Mark
 
[snip]
I'm struggling to work out why this seems to be so difficult...isn't a
MailMessage object just an object in memory? Why does it seem to be so
difficult to get an array of its bytes? This ought to be the easiest
thing in the world, something like (again, apologies for the C#)...

I was just looking through the documentation, and found this property:
MailMessage.Fields. It returns an IDictionary.
There's no documentation for it, though, except that it needs Framework 1.1.
I don't have VS2003, so I can't test it, but it looks promising...
If it returned all the MailMessage fields, you could do this:

MemoryStream objMS = new MemoryStream();
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMS, new HashTable(objMS.Fields));

I like C# more, anyway. :-)

[snip]
 
[snip]
MemoryStream objMS = new MemoryStream();
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMS, new HashTable(objMS.Fields));

Oops, I messed that up, here's what it should be:
MailMessage objMailMessage = new MailMessage();
MemoryStream objMS = new MemoryStream();
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMS, new Hashtable(objMailMessage.Fields));
byte[] abytMailMessage = objMS.GetBuffer();
I like C# more, anyway. :-)

[snip]
 
Pete,

Well, we're definitely getting there! I have the following working:

MailMessage objMailMessage = new MailMessage();

objMailMessage.From = "(e-mail address removed)";
objMailMessage.To = "(e-mail address removed)";

MemoryStream objMS = new MemoryStream();
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMS, new Hashtable(objMailMessage.Fields));
byte[] abytMailMessage = objMS.GetBuffer();
string strMessage = Convert.ToBase64String(abytMailMessage);

strMessage has a length of 344, and looks like this:
AAEAAAD/////AQAAAAAAAAAEAQAAABxTeXN0ZW0uQ29sbGVjdGlvbnMuSGFzaHRhYmxlBwAAAApM
b2FkRmFjdG9yB1ZlcnNpb24IQ29tcGFyZXIQSGFzaENvZGVQcm92aWRlcghIYXNoU2l6ZQRLZXlz
BlZhbHVlcwAAAwMABQULCBxTeXN0ZW0uQ29sbGVjdGlvbnMuSUNvbXBhcmVyJFN5c3RlbS5Db2xs
ZWN0aW9ucy5JSGFzaENvZGVQcm92aWRlcgjsUTg/AAAAAAoKCwAAAAkCAAAACQMAAAAQAgAAAAAA
AAAQAwAAAAAAAAALAAAAAAAAAAAAAAAAAAAAAA==

Very nice! However, I then try to pass this string to the WebMethod as
follows:

bool bReturn = cwsEmail.SendMailDotNet(strMessage);

The WebMethod is as follows:

[WebMethod]
public bool SendMailDotNet(string strMailMessage)
{
// unstitch the string back into a MailMessage
// haven't written this yet
return true;
}

However, when I interrogate strMessage, it is null - very annoying... :-(
Am obviously doing something wrong...

Mark
 
Pete,
Well, we're definitely getting there! I have the following working:

Back to the drawing board! Seems the MailMessage .Fields does not expose the
underlying MailMessage properties, but is used to add additional properties
to MailMessage object such as username, password, port etc...

Sigh... :-(

Mark
 
Back
Top