473,511 Members | 10,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting an object to a byte array

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 received.

Best regards,

Mark Rae
Nov 15 '05 #1
5 7558
In article <ei**************@tk2msftngp13.phx.gbl>, ma**@markrae.co.uk
says...
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 received.


You should be able to use the BinaryFormatter and serialization to
serialize it to a MemoryStream. From there, use MemoryStream.GetBuffer
to obtain the byte[].

Finally, try Convert.ToBase64 on the byte array.

Of course, the serialized data will only make sense to another .NET
client that can deserialize it... :)

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #2
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...

Hi Patrick,

Thanks for the reply.
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 received.


You should be able to use the BinaryFormatter and serialization to
serialize it to a MemoryStream.


According to Joe Mayo, the MailMessage type is not serializable...

Best,

Mark
Nov 15 '05 #3
"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
According to Joe Mayo, the MailMessage type is not serializable...


This is the code I'm trying to use:

MailMessage objMailMessage = new MailMessage();
try
{
MemoryStream objMS = new MemoryStream();
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMS, objMailMessage); // error
thrown on this line
byte[] abytMailMessage = objMS.GetBuffer();
}
catch (Exception ex)
{
//write to EventLog
throw(ex);
}

ex.ToString() reveals the error details below:

System.Runtime.Serialization.SerializationExceptio n: The type
System.Web.Mail.MailMessage in Assembly System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a is not marked as
serializable.\r\n at
System.Runtime.Serialization.FormatterServices.Int ernalGetSerializableMember
s(RuntimeType type, Boolean excludeNonSerializable)\r\n at
System.Runtime.Serialization.FormatterServices.Get SerializableMembers(Type
type, StreamingContext context)\r\n at
System.Runtime.Serialization.Formatters.Binary.Wri teObjectInfo.InitMemberInf
o()\r\n at
System.Runtime.Serialization.Formatters.Binary.Wri teObjectInfo.InitSerialize
(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)\r\n at
System.Runtime.Serialization.Formatters.Binary.Wri teObjectInfo.Serialize(Obj
ect obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)\r\n at
System.Runtime.Serialization.Formatters.Binary.Obj ectWriter.Serialize(Object
graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)\r\n at
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Serialize(Str
eam serializationStream, Object graph, Header[] headers, Boolean fCheck)\r\n
at
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Serialize(Str
eam serializationStream, Object graph)\r\n at
TestEmail.WebForm1.Page_Load(Object sender, EventArgs e) in
c:\\inetpub\\wwwroot\\testemail\\webform1.aspx.cs: line 29"
Nov 15 '05 #4
In article <#v**************@TK2MSFTNGP11.phx.gbl>, ma**@markrae.co.uk
says...
You should be able to use the BinaryFormatter and serialization to
serialize it to a MemoryStream.


According to Joe Mayo, the MailMessage type is not serializable...


Didn't check that before posting. Since it's not serializable, you're
kind of out of luck.

The next thing to do would be to back up a few steps and ask *why* do
you need the binary representation of a MailMessage encoded to a base64
string?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #5
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
The next thing to do would be to back up a few steps and ask *why* do
you need the binary representation of a MailMessage encoded to a base64
string?


Easy - because my client has asked me to investigate if it can be done!
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
13900
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
2654
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
16447
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...
4
3868
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
4533
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
7319
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...
8
4178
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
5
8968
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...
2
11563
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.