如何从邮箱地址字符串获取Outlook.AddressEntry对象并配置C# VSTO发件邮箱?
Hey there! Let's break down how to handle both of your needs in your C# VSTO Outlook project—setting a MailItem's sender from an email string, and grabbing the corresponding Outlook.AddressEntry object.
You can't directly assign a string email to the Sender property (it expects an AddressEntry object), so we first need to resolve the email to an AddressEntry, then set it. We can also set SendUsingAccount to ensure the email sends from the correct account (this is often necessary to avoid default account overrides).
Here's how to update your existing code:
// Your existing code to create the mail item Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)Globals.ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); mailItem.Subject = "Order #1"; mailItem.To = "orderbot@example.com"; string targetSenderEmail = "john@doe.com"; // Get the AddressEntry for the sender email var senderAddressEntry = GetAddressEntryFromEmail(targetSenderEmail); if (senderAddressEntry != null) { // Set the Sender property mailItem.Sender = senderAddressEntry; // Optional but recommended: Set the account to send from var senderAccount = GetAccountFromEmail(targetSenderEmail); if (senderAccount != null) { mailItem.SendUsingAccount = senderAccount; } }
To get the AddressEntry from an email string, we can use Outlook's built-in recipient resolution first (which checks the Global Address List and your contacts), then fall back to searching your contacts manually if needed.
Add this helper method to your project:
private Microsoft.Office.Interop.Outlook.AddressEntry GetAddressEntryFromEmail(string email) { try { // First, try resolving the email via Outlook's built-in resolver var recipient = Globals.ThisAddIn.Application.Session.CreateRecipient(email); if (recipient.Resolve()) { return recipient.AddressEntry; } // If resolution fails, search all contact folders for the email var defaultContactsFolder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts); foreach (Microsoft.Office.Interop.Outlook.MAPIFolder subFolder in defaultContactsFolder.Folders) { foreach (var item in subFolder.Items) { if (item is Microsoft.Office.Interop.Outlook.ContactItem contact) { // Check all three email fields for a match if (string.Equals(contact.Email1Address, email, StringComparison.OrdinalIgnoreCase) || string.Equals(contact.Email2Address, email, StringComparison.OrdinalIgnoreCase) || string.Equals(contact.Email3Address, email, StringComparison.OrdinalIgnoreCase)) { return contact.AddressEntry; } } } } // If no match found return null; } catch (Exception ex) { // Log or handle the error as needed System.Diagnostics.Debug.WriteLine($"Failed to get AddressEntry: {ex.Message}"); return null; } }
Bonus: Get the Account for the Sender Email
If you need to set SendUsingAccount (to ensure the email sends from the correct account), use this helper method:
private Microsoft.Office.Interop.Outlook.Account GetAccountFromEmail(string email) { foreach (var account in Globals.ThisAddIn.Application.Session.Accounts) { if (string.Equals(account.SmtpAddress, email, StringComparison.OrdinalIgnoreCase)) { return account; } } return null; }
Key Notes
- The
CreateRecipient.Resolve()method works best for Exchange/GAL emails, but may fail for external addresses not saved in your contacts—hence the manual contact search fallback. - Always check for
nullvalues after resolving, as the email might not exist in Outlook's address lists. - Make sure your VSTO add-in has the necessary permissions to access Outlook's address books and contacts.
内容的提问来源于stack exchange,提问作者miny1997

