469,963 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Converting an unserializeable object to a byte array

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
Nov 20 '05 #1
12 7216
Mark Rae wrote:
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
Nov 20 '05 #2
"AirPete" <x@x.x> wrote in message
news:%Q******************@newsread2.news.pas.earth link.net...

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
Nov 20 '05 #3
"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
MailMessage objMailMessage = new MailMessage();
byte[] = System.Text.ASCII.Encoding.GetBytes(objMailMessage );


Apologies for the above being in C#...:-)
Nov 20 '05 #4
Mark Rae wrote:
"AirPete" <x@x.x> wrote in message
news:%Q******************@newsread2.news.pas.earth link.net...

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
Nov 20 '05 #5
"AirPete" <x@x.x> wrote in message
news:qx******************@newsread1.news.pas.earth link.net...

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
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());


Nov 20 '05 #6
Mark Rae wrote:
"AirPete" <x@x.x> wrote in message
news:qx******************@newsread1.news.pas.earth link.net...

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]

Nov 20 '05 #7
"AirPete" <x@x.x> wrote in message
news:_S******************@newsread1.news.pas.earth link.net...

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
Nov 20 '05 #8
[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]
Nov 20 '05 #9
[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]


Nov 20 '05 #10
"AirPete" <x@x.x> wrote in message
news:X_******************@newsread1.news.pas.earth link.net...

Pete,

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

MailMessage objMailMessage = new MailMessage();

objMailMessage.From = "mr**@spml-dev.co.uk";
objMailMessage.To = "mr**@spml-dev.co.uk";

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/////AQAAAAAAAAAEAQAAABxTeXN0ZW0uQ29sbGVjdGlvbnMuSGFzaH RhYmxlBwAAAApM
b2FkRmFjdG9yB1ZlcnNpb24IQ29tcGFyZXIQSGFzaENvZGVQcm 92aWRlcghIYXNoU2l6ZQRLZXlz
BlZhbHVlcwAAAwMABQULCBxTeXN0ZW0uQ29sbGVjdGlvbnMuSU NvbXBhcmVyJFN5c3RlbS5Db2xs
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
Nov 20 '05 #11
"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:uf*************@TK2MSFTNGP11.phx.gbl...

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
Nov 20 '05 #12
From a fairly cursory examination, this article might be what you're looking
for: -

http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Nick Hall

"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:eB**************@tk2msftngp13.phx.gbl...
"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:uf*************@TK2MSFTNGP11.phx.gbl...

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

Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by matt melton | last post: by
4 posts views Thread by Joseph Suprenant | last post: by
2 posts views Thread by Tomas Deman | last post: by
5 posts views Thread by Mark Rae | last post: by
4 posts views Thread by Svetoslav Vasilev | last post: by
5 posts views Thread by Lint Radley | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.