When setting properties (CreationDate, FileName, ModificationDate, ReadDate) of ContentDisposition
is automatically performed the addition of the respective element to the list of parameters.
Subsequently, while copying the parameters you need to check if they are not already present.
Currently this missing check generates an exception during deserialization.
Code example :
public static void Serialize(MailMessage message, string file)
{
using (var stream = new FileStream(file, FileMode.Create))
{
new BinaryFormatter().Serialize(stream, (SerializableMailMessage)message);
}
}
public static MailMessage Deserialize(string file)
{
using (var stream = File.OpenRead(file))
{
return (SerializableMailMessage)new BinaryFormatter().Deserialize(stream);
}
}
static void Main(string[] args)
{
var mail = new MailMessage();
var attachment = new Attachment(@"c:\foo.pdf");
attachment.ContentDisposition.CreationDate = DateTime.Now; // <-- set property
mail.Attachments.Add(attachment);
mail.From = new MailAddress("from@domain.com");
mail.To.Add("to@domain.com");
mail.Subject = "subject";
mail.Body = "body";
Serialize(mail, @"c:\mail.bin");
var tmp = Deserialize(@"c:\mail.bin"); // <-- System.ArgumentException
}
Exception details :
System.ArgumentException was unhandled
Message=An item with the same key has already been added.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at System.Net.TrackingValidationObjectDictionary.PersistValue(String key, String value, Boolean addValue)
at System.Net.TrackingValidationObjectDictionary.Add(String key, String value)
at S22.Mail.SerializableAttachment.op_Implicit(SerializableAttachment attachment)
at S22.Mail.SerializableMailMessage.op_Implicit(SerializableMailMessage message)
at ConsoleApplication1.Program.Deserialize(String file)
at ConsoleApplication1.Program.Main(String[] args)
[...]
InnerException:
When setting properties (CreationDate, FileName, ModificationDate, ReadDate) of ContentDisposition
is automatically performed the addition of the respective element to the list of parameters.
Subsequently, while copying the parameters you need to check if they are not already present.
Currently this missing check generates an exception during deserialization.
Code example :
Exception details :