473,238 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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 7656
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
2
by: Tomas Deman | last post by:
Hi, I need a fast method for converting an int array to a byte array. At the moment, I'm using this: public static byte Int2ByteArray(int array) { byte lbytRetval = new byte; int lintIdxHi;...
5
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an 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...
4
by: Svetoslav Vasilev | last post by:
Hi, I experience some troubles trying to convert an object,returned by a DataTable for a field value to its actual representation of Byte. The field values in a DataTable as we all know are returned...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
2
by: Matt | last post by:
I just wanted to know if I am converting to/from streams the easiest and correct way. I am performing the following statements to convert byte arrays to and from streams during different...
5
by: Lint Radley | last post by:
Hello, I am looking for a way to cast a byte to Int16. I get this error: "Unable to cast object of type 'System.Byte' to type 'System.Int16'" If you're curious, I am using a library for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.