Converting an 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 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
 
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... :)
 
Hi Patrick,

Thanks for the reply.
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
 
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.SerializationException: 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.InternalGetSerializableMember
s(RuntimeType type, Boolean excludeNonSerializable)\r\n at
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type
type, StreamingContext context)\r\n at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInf
o()\r\n at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize
(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)\r\n at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Obj
ect obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)\r\n at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object
graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)\r\n at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Str
eam serializationStream, Object graph, Header[] headers, Boolean fCheck)\r\n
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.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"
 
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?
 
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!
 
Back
Top