rgl / MailBounceDetector

Detects whether a MailKit email Message is a bounce message
MIT License
28 stars 12 forks source link

Unable to cast object of type 'MimeKit.TextPart' to type 'MimeKit.MessagePart' #5

Open p231970 opened 7 years ago

p231970 commented 7 years ago

I have this System.InvalidCastException: Unable to cast object of type 'MimeKit.TextPart' to type 'MimeKit.MessagePart'. at MailBounceDetector.BounceDetectResult..ctor(MimeMessage message, MimeEntity deliveryNotification, MessageDeliveryStatus deliveryStatus, MimeEntity undeliveredMessagePart) at MailBounceDetector.StandardBounceDetector.Detect(MimeMessage message) at MailBounceDetector.BounceDetector.Detect(MimeMessage message)

p231970 commented 7 years ago

I think issue is in file BounceDetectResult.cs on line 182 look this screenshot http://prntscr.com/e12wlv

rgl commented 7 years ago

Please, create a (failing) unit test that triggers this behavior.

p231970 commented 7 years ago

I am new to GitHub so sorry for maybe wrong editing. I hope you'll find all my proposed changes in few patches

ToddHedenstrom commented 7 years ago

I'm running into the same issue. In debugging down to this, the UndeliveredMessagePart does not contain a member of 'Message', so the exception is thrown. I'd like to help out with this, but haven't got any idea how to create a failing unit test. I have a couple of these bounces in a folder on an inbox, I can forward them to you- they are AOL bounces.

For the time being, I've just commented this segment out, as I'm not interested in the UndeliveredMessageId, but I know this isn't something that's a good option for most.

edinj commented 7 years ago

In addition to ToddHedenstrom's comment I added extension method:

public static bool HasMethod(this object objectToCheck, string methodName) { var type = objectToCheck.GetType(); return type.GetMethod(methodName) != null; }

And changed line 179 to:

if (UndeliveredMessagePart != null && UndeliveredMessagePart.HasMethod("Message"))

Now works well with message id.