public String GetRecipientEmail(Recipient recipient) { String retEmail = ""; log.Fine("Determining email of recipient: " + recipient.Name); AddressEntry addressEntry; try { addressEntry = recipient.AddressEntry; } catch { log.Warn("Can't resolve this recipient!"); addressEntry = null; } if (addressEntry == null) { log.Warn("No AddressEntry exists!"); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name); EmailAddress.IsValidEmail(retEmail); return(retEmail); } log.Fine("AddressEntry Type: " + recipient.AddressEntry.Type); if (recipient.AddressEntry.Type == "EX") //Exchange { log.Fine("Address is from Exchange"); retEmail = ADX_GetSMTPAddress(recipient.AddressEntry.Address); } else if (recipient.AddressEntry.Type.ToUpper() == "NOTES") { log.Fine("From Lotus Notes"); //Migrated contacts from notes, have weird "email addresses" eg: "James T. Kirk/US-Corp03/enterprise/US" retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name); } else { log.Fine("Not from Exchange"); retEmail = recipient.AddressEntry.Address; } if (retEmail == null || retEmail == String.Empty || retEmail == "" || retEmail == "Unknown") { log.Error("Failed to get email address through Addin MAPI access!"); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name); } if (retEmail.IndexOf("<") > 0) { retEmail = retEmail.Substring(retEmail.IndexOf("<") + 1); retEmail = retEmail.TrimEnd(Convert.ToChar(">")); } log.Fine("Email address: " + retEmail, retEmail); EmailAddress.IsValidEmail(retEmail); return(retEmail); }
public String GetRecipientEmail(Recipient recipient) { String retEmail = ""; log.Fine("Determining email of recipient: " + recipient.Name); AddressEntry addressEntry; try { addressEntry = recipient.AddressEntry; } catch { log.Warn("Can't resolve this recipient!"); addressEntry = null; } if (addressEntry == null) { log.Warn("No AddressEntry exists!"); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name); EmailAddress.IsValidEmail(retEmail); return(retEmail); } log.Fine("AddressEntry Type: " + recipient.AddressEntry.Type); if (recipient.AddressEntry.Type == "EX") //Exchange { log.Fine("Address is from Exchange"); if (recipient.AddressEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry || recipient.AddressEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry) { ExchangeUser eu = recipient.AddressEntry.GetExchangeUser(); if (eu != null && eu.PrimarySmtpAddress != null) { retEmail = eu.PrimarySmtpAddress; } else { log.Warn("Exchange does not have an email for recipient: " + recipient.Name); try { //Should I try PR_EMS_AB_PROXY_ADDRESSES next to cater for cached mode? Microsoft.Office.Interop.Outlook.PropertyAccessor pa = recipient.PropertyAccessor; retEmail = pa.GetProperty(OutlookNew.PR_SMTP_ADDRESS).ToString(); log.Debug("Retrieved from PropertyAccessor instead."); } catch { log.Warn("Also failed to retrieve email from PropertyAccessor."); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name); } } } else if (recipient.AddressEntry.AddressEntryUserType == OlAddressEntryUserType.olOutlookContactAddressEntry) { log.Fine("This is an Outlook contact"); ContactItem contact = null; try { contact = recipient.AddressEntry.GetContact(); } catch { log.Warn("Doesn't seem to be a valid contact object. Maybe this account is not longer in Exchange."); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name); } if (contact != null) { if (contact.Email1AddressType == "EX") { log.Fine("Address is from Exchange."); log.Fine("Using PropertyAccessor to get email address."); Microsoft.Office.Interop.Outlook.PropertyAccessor pa = contact.PropertyAccessor; retEmail = pa.GetProperty(EMAIL1ADDRESS).ToString(); } else { retEmail = contact.Email1Address; } } } else { log.Fine("Exchange type: " + recipient.AddressEntry.AddressEntryUserType.ToString()); log.Fine("Using PropertyAccessor to get email address."); Microsoft.Office.Interop.Outlook.PropertyAccessor pa = recipient.PropertyAccessor; retEmail = pa.GetProperty(OutlookNew.PR_SMTP_ADDRESS).ToString(); } } else if (recipient.AddressEntry.Type.ToUpper() == "NOTES") { log.Fine("From Lotus Notes"); //Migrated contacts from notes, have weird "email addresses" eg: "James T. Kirk/US-Corp03/enterprise/US" retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name); } else { log.Fine("Not from Exchange"); retEmail = recipient.AddressEntry.Address; } if (retEmail.IndexOf("<") > 0) { retEmail = retEmail.Substring(retEmail.IndexOf("<") + 1); retEmail = retEmail.TrimEnd(Convert.ToChar(">")); } log.Fine("Email address: " + retEmail, retEmail); EmailAddress.IsValidEmail(retEmail); return(retEmail); }
public String GetRecipientEmail(Recipient recipient) { String retEmail = ""; Boolean builtFakeEmail = false; log.Fine("Determining email of recipient: " + recipient.Name); AddressEntry addressEntry = null; try { try { addressEntry = recipient.AddressEntry; } catch { log.Warn("Can't resolve this recipient!"); addressEntry = null; } if (addressEntry == null) { log.Warn("No AddressEntry exists!"); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } else { log.Fine("addressEntry Type: " + addressEntry.Type); if (addressEntry.Type == "EX") //Exchange { log.Fine("Address is from Exchange"); retEmail = ADX_GetSMTPAddress(addressEntry.Address); } else if (addressEntry.Type != null && addressEntry.Type.ToUpper() == "NOTES") { log.Fine("From Lotus Notes"); //Migrated contacts from notes, have weird "email addresses" eg: "James T. Kirk/US-Corp03/enterprise/US" retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } else { log.Fine("Not from Exchange"); try { if (string.IsNullOrEmpty(addressEntry.Address)) { log.Warn("addressEntry.Address is empty."); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } else { retEmail = addressEntry.Address; } } catch (System.Exception ex) { log.Error("Failed accessing addressEntry.Address"); log.Error(ex.Message); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } } } if (string.IsNullOrEmpty(retEmail) || retEmail == "Unknown") { log.Error("Failed to get email address through Addin MAPI access!"); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } if (retEmail != null && retEmail.IndexOf("<") > 0) { retEmail = retEmail.Substring(retEmail.IndexOf("<") + 1); retEmail = retEmail.TrimEnd(Convert.ToChar(">")); } log.Fine("Email address: " + retEmail, retEmail); if (!EmailAddress.IsValidEmail(retEmail) && !builtFakeEmail) { retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); if (!EmailAddress.IsValidEmail(retEmail)) { MainForm.Instance.Logboxout("ERROR: Recipient \"" + recipient.Name + "\" with email address \"" + retEmail + "\" is invalid.", notifyBubble: true); MainForm.Instance.Logboxout("This must be manually resolved in order to sync this appointment."); throw new ApplicationException("Invalid recipient email for \"" + recipient.Name + "\""); } } return(retEmail); } finally { addressEntry = (AddressEntry)OutlookCalendar.ReleaseObject(addressEntry); } }
public String GetRecipientEmail(Recipient recipient) { String retEmail = ""; Boolean builtFakeEmail = false; log.Fine("Determining email of recipient: " + recipient.Name); AddressEntry addressEntry = null; String addressEntryType = ""; try { try { addressEntry = recipient.AddressEntry; } catch { log.Warn("Can't resolve this recipient!"); addressEntry = null; } if (addressEntry == null) { log.Warn("No AddressEntry exists!"); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } else { try { addressEntryType = addressEntry.Type; } catch { log.Warn("Cannot access addressEntry.Type!"); } log.Fine("AddressEntry Type: " + addressEntryType); if (addressEntryType == "EX") //Exchange { log.Fine("Address is from Exchange"); if (addressEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry || addressEntry.AddressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry) { ExchangeUser eu = null; try { eu = addressEntry.GetExchangeUser(); if (eu != null && eu.PrimarySmtpAddress != null) { retEmail = eu.PrimarySmtpAddress; } else { log.Warn("Exchange does not have an email for recipient: " + recipient.Name); Microsoft.Office.Interop.Outlook.PropertyAccessor pa = null; try { //Should I try PR_EMS_AB_PROXY_ADDRESSES next to cater for cached mode? pa = recipient.PropertyAccessor; retEmail = pa.GetProperty(OutlookNew.PR_SMTP_ADDRESS).ToString(); log.Debug("Retrieved from PropertyAccessor instead."); } catch { log.Warn("Also failed to retrieve email from PropertyAccessor."); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } finally { pa = (Microsoft.Office.Interop.Outlook.PropertyAccessor)OutlookCalendar.ReleaseObject(pa); } } } finally { eu = (ExchangeUser)OutlookCalendar.ReleaseObject(eu); } } else if (addressEntry.AddressEntryUserType == OlAddressEntryUserType.olOutlookContactAddressEntry) { log.Fine("This is an Outlook contact"); ContactItem contact = null; try { try { contact = addressEntry.GetContact(); } catch { log.Warn("Doesn't seem to be a valid contact object. Maybe this account is no longer in Exchange."); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } if (contact != null) { if (contact.Email1AddressType == "EX") { log.Fine("Address is from Exchange."); log.Fine("Using PropertyAccessor to get email address."); Microsoft.Office.Interop.Outlook.PropertyAccessor pa = null; try { pa = contact.PropertyAccessor; retEmail = pa.GetProperty(EMAIL1ADDRESS).ToString(); } finally { pa = (Microsoft.Office.Interop.Outlook.PropertyAccessor)OutlookCalendar.ReleaseObject(pa); } } else { retEmail = contact.Email1Address; } } } finally { contact = (ContactItem)OutlookCalendar.ReleaseObject(contact); } } else { log.Fine("Exchange type: " + addressEntry.AddressEntryUserType.ToString()); log.Fine("Using PropertyAccessor to get email address."); Microsoft.Office.Interop.Outlook.PropertyAccessor pa = null; try { pa = recipient.PropertyAccessor; retEmail = pa.GetProperty(OutlookNew.PR_SMTP_ADDRESS).ToString(); } finally { pa = (Microsoft.Office.Interop.Outlook.PropertyAccessor)OutlookCalendar.ReleaseObject(pa); } } } else if (addressEntryType.ToUpper() == "NOTES") { log.Fine("From Lotus Notes"); //Migrated contacts from notes, have weird "email addresses" eg: "James T. Kirk/US-Corp03/enterprise/US" retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } else { log.Fine("Not from Exchange"); try { if (string.IsNullOrEmpty(addressEntry.Address)) { log.Warn("addressEntry.Address is empty."); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } else { retEmail = addressEntry.Address; } } catch (System.Exception ex) { log.Error("Failed accessing addressEntry.Address"); log.Error(ex.Message); retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); } } } if (retEmail != null && retEmail.IndexOf("<") > 0) { retEmail = retEmail.Substring(retEmail.IndexOf("<") + 1); retEmail = retEmail.TrimEnd(Convert.ToChar(">")); } log.Fine("Email address: " + retEmail, retEmail); if (!EmailAddress.IsValidEmail(retEmail) && !builtFakeEmail) { retEmail = EmailAddress.BuildFakeEmailAddress(recipient.Name, out builtFakeEmail); if (!EmailAddress.IsValidEmail(retEmail)) { MainForm.Instance.Logboxout("ERROR: Recipient \"" + recipient.Name + "\" with email address \"" + retEmail + "\" is invalid.", notifyBubble: true); MainForm.Instance.Logboxout("This must be manually resolved in order to sync this appointment."); throw new ApplicationException("Invalid recipient email for \"" + recipient.Name + "\""); } } return(retEmail); } finally { addressEntry = (AddressEntry)OutlookCalendar.ReleaseObject(addressEntry); } }