EWS Managed API:能否为邮件分配外部GUID用于后续搜索?
Absolutely, you can assign a custom GUID/identifier to an EmailMessage for later search purposes—here's a practical breakdown of how to make this work, aligned with your example code:
1. Store the GUID in the Email Object
First, you need a way to attach the GUID to your email. How you do this depends on whether you're using a custom EmailMessage class or a third-party library:
- If you've built your own
EmailMessagewrapper (like your example shows), simply add anExternalGuidproperty (typeGuidorstring) to the class. - If using a standard email library (e.g., MailKit, Exchange Web Services), use a custom email header (like
X-External-Guid) to store the GUID. This ensures the identifier travels with the email and gets saved on the server.
2. Assign the GUID & Send the Email
Custom Wrapper Class Scenario
This matches the code structure you provided. You'll want to persist a mapping between the GUID and the email's system-generated ID (for easy later lookup):
// Generate a unique GUID var externalGuid = Guid.NewGuid(); // Initialize your email and assign the GUID var email = new EmailMessage(_service); email.ExternalGuid = externalGuid; // Send the email, then save the GUID-to-email mapping (e.g., in a database) _service.Send(email); _service.SaveEmailMapping(externalGuid, email.SystemId); // Custom method to store the relationship
Third-Party Library Scenario (e.g., MailKit)
Use a custom header to embed the GUID directly in the email:
var externalGuid = Guid.NewGuid(); var message = new MimeMessage(); // Add custom header with your GUID message.Headers.Add("X-External-Guid", externalGuid.ToString()); // Send the email as usual using (var client = new SmtpClient()) { await client.ConnectAsync("smtp.yourdomain.com", 587, SecureSocketOptions.StartTls); await client.AuthenticateAsync("your-email@domain.com", "your-password"); await client.SendAsync(message); await client.DisconnectAsync(true); }
3. Check If the Email Exists Using the GUID
Custom Wrapper + Database Mapping
Query your stored mapping to verify existence:
public bool IsExistByExternalGuid(Guid externalGuid) { // Check if the GUID exists in your mapping table return _dbContext.EmailMappings.Any(mapping => mapping.ExternalGuid == externalGuid); }
Third-Party Library + Custom Header
Search the email server for messages containing your custom header:
// Example using Exchange Web Services (EWS) public bool IsExistByExternalGuid(Guid externalGuid) { // Create a filter to search for the custom header var headerFilter = new SearchFilter.ContainsSubstring( ItemSchema.InternetMessageHeaders, $"X-External-Guid: {externalGuid}" ); // Search the Sent Items folder (adjust folder as needed) var searchResults = _service.FindItems(WellKnownFolderName.SentItems, headerFilter, new ItemView(1)); // Return true if any matching email is found return searchResults.TotalCount > 0; }
Key Notes
- Ensure uniqueness: Always generate GUIDs with
Guid.NewGuid()to avoid collisions. - Flexible type: If sharing the GUID across systems, use a string representation to avoid type conversion issues.
- Server compatibility: Most mainstream email servers support custom headers, but double-check if you're using a niche service.
内容的提问来源于stack exchange,提问作者eocron

