如何通过OLE Outlook获取退回邮件诊断信息(Gupta/TeamDeveloper)
I’ve dealt with similar headaches when working with Outlook OLE in TeamDeveloper, so let’s break down how to safely identify non-delivery reports (NDRs) caused by invalid recipients, while avoiding crashes from empty date fields.
1. First, Identify NDRs Using Outlook’s Message Class
Outlook tags bounce messages with a specific MessageClass property. For standard "recipient not found" NDRs, this is usually IPM.Note.NDR (for non-delivery reports). Start by checking this property before touching date fields—it’s a reliable way to separate bounces from regular emails upfront:
strMsgClass = OutlookItem.MessageClass IF strMsgClass = "IPM.Note.NDR" THEN // This is likely a bounce message - handle it specially bIsNDR = TRUE END IF
2. Safely Read Date Fields (or Avoid Them Altogether)
The crash happens because Outlook doesn’t populate ReceivedTime or SentOn for some NDRs, and TeamDeveloper’s OLE handler throws an error when trying to read an empty date value. Here are two solid fixes:
Option A: Wrap Date Reads in Error Handling
Use a TRY...CATCH block to catch the OLE error gracefully instead of letting it crash your app:
TRY dtReceived = OutlookItem.ReceivedTime // If we get here, the date is valid - proceed with normal logic CATCH OLE_ERROR // Empty date detected - flag this as an NDR bIsNDR = TRUE END TRY
Option B: Use Outlook’s PropertyAccessor for Safe Checks
Outlook’s PropertyAccessor lets you verify if a property exists and has a value before reading it. For the ReceivedTime property, use its MAPI property tag (0x0E060040):
objPropAccessor = OutlookItem.PropertyAccessor IF objPropAccessor.PropertyExists("http://schemas.microsoft.com/mapi/proptag/0x0E060040") THEN dtReceived = objPropAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E060040") ELSE // Property is missing/empty - mark as NDR bIsNDR = TRUE END IF
This is more robust because it proactively checks for the property’s existence instead of reacting to an error.
3. Add Backup Checks for NDRs
Date checks alone aren’t foolproof. Combine them with other NDR indicators to be 100% sure you’re targeting the right bounces:
- Subject Line: Look for keywords like
Undeliverable:(English) or localized equivalents (e.g.,无法送达:for Chinese) - Sender Address: NDRs usually come from system addresses like
postmaster@yourdomain.comormailer-daemon@xxx.com - Body Content: Search for SMTP error codes like
550 5.1.1(the standard code for "recipient does not exist")
Example of a subject check in TeamDeveloper:
strSubject = OutlookItem.Subject IF InStr(strSubject, "Undeliverable:") > 0 THEN bIsNDR = TRUE END IF
Final Workflow
Putting it all together, your logic should follow this order:
- Check the
MessageClassto flag potential NDRs - Use error handling or
PropertyAccessorto safely validate date fields (no more crashes) - Add secondary checks (subject, sender, body) to confirm it’s a "recipient not found" bounce
This approach eliminates crash risks and gives you a far more accurate way to identify the specific bounces you care about.
内容的提问来源于stack exchange,提问作者Flo

