Sicos1977 / MSGReader

C# Outlook MSG file reader without the need for Outlook
http://sicos1977.github.io/MSGReader
MIT License
476 stars 168 forks source link

Argument Null Exception when saving an EML after removing an attachement. #364

Closed sven-peeters-stitchd-be closed 12 months ago

sven-peeters-stitchd-be commented 1 year ago

When removing an attcahement from a loaded EML, it throws an argument null exception during the Save operation.

var msg = MsgReader.Mime.Message.Load(new FileInfo("C:\\Users\\A1055101\\Downloads\\test.eml"));
msg.Attachments.RemoveAt(1);
msg.Save(new FileInfo("C:\\Users\\A1055101\\Downloads\\test_clean2.eml"));

"Value cannot be null. (Parameter 'value')" at System.Net.Mime.MimeBasePart.set_ContentType(ContentType value) at MsgReader.Mime.Message.ToMailMessage() at MsgReader.Mime.Message.Save(Stream messageStream) at MsgReader.Mime.Message.Save(FileInfo file) at Program.

$(String[] args) in C:\Users\A1055101\source\repos\DocaMonTest\DocaMonTest\Program.cs:line 57

Sicos1977 commented 1 year ago

Weird I had a check for it in the code and then resharper complained it was not needed so I removed it. And now the code gives that error :-) .... Always nice a tool that has a split personality

Sicos1977 commented 1 year ago

See version 5.4.3 .... also test now :-)

Sicos1977 commented 1 year ago

There is no save method in the MailMessage class so I had to use a reflection example from the internet but as usual this works different if you are on the .net framework or .net 5 and 6 so I had to build that in to.... so it took me a while :-)

sven-peeters-stitchd-be commented 1 year ago

Throwing a different error now :-( Using .Net6

NullReferenceException: Object reference not set to an instance of an object. This exception was originally thrown at this call stack: System.Net.Mail.Message.PrepareEnvelopeHeaders(bool, bool) System.Net.Mail.Message.Send(System.Net.Mime.BaseWriter, bool, bool) System.Net.Mail.MailMessage.Send(System.Net.Mime.BaseWriter, bool, bool)

Sicos1977 commented 1 year ago

It is probably because the reflection works somewhat different in .net 6. I'll have to make a .net 6 test app because the app I used is .net core 3 so there is probably a difference between those .net versions. I'll try to look into this tomorrow today I have to finished something else.

Sicos1977 commented 1 year ago

It seems not to be the reflection, I just updated the test project to also target .net 6 and this one worked without any problems with the em file I used

Sicos1977 commented 1 year ago

I think it is best that you clone my repository to your local development machine and debug the code yourself to see what is going wrong with your eml file.

Sicos1977 commented 12 months ago

No response.... closed