Get Smtp email from ContactInfo stored in Exchange - outlook

I am using VSTO for my Outlook add-in. Currently I am processing email addresses from all Outlook contacts. There is no problem for instances of ContactInfo if EmailAddress1Type is "SMTP".
But how to get email address for Exchange contact (Email1AddressType = "EX")?
Redemption library is not solution for me as it is expensive just to solve this one problem.
Thank you in advance,
Dusan

Here is the MSDN reference link and corresponding sample code:
private const string Email1EntryIdPropertyAccessor = "http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80850102";
PropertyAccessor propertyAccessor = contactItem.PropertyAccessor;
object rawPropertyValue = propertyAccessor.GetProperty(Email1EntryIdPropertyAccessor);
string recipientEntryID = propertyAccessor.BinaryToString(rawPropertyValue);
Recipient recipient = contactItem.Application.Session.GetRecipientFromID(recipientEntryID);
if (null == recipient)
throw new InvalidOperationException();
bool wasResolved = recipient.Resolve();
if (!wasResolved)
throw new InvalidOperationException();
ExchangeUser exchangeUser = recipient.AddressEntry.GetExchangeUser();
string smtpAddress = exchangeUser.PrimarySmtpAddress;

Related

Check if current user is a member of exchange distribution list - Outlook C#

I want to find out if current Outlook user is a member of particular exchange distribution list. If he is, then he should see child form and if he isn't; then he should see message box.
My following code is working up to the point, if user is a member of DistList, he get child form but I don't know how to check show him message box if he isn't member.
string UserName = (string)application.ActiveExplorer().Session.CurrentUser.Name;
string PersonalPublicFolder = "Public Folders - " + application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
Outlook.MAPIFolder contactsFolder = outlookNameSpace.Folders[PersonalPublicFolder].Folders["Favorites"];
Outlook.DistListItem addressList = contactsFolder.Items["ContactGroup"];
if (addressList.MemberCount != 0)
{
for (int i = 1; i <= addressList.MemberCount; i++)
{
Outlook.Recipient recipient = addressList.GetMember(i);
string contact = recipient.Name;
if (contact == UserName)
{
var assignOwnership = new AssignOwnership();
assignOwnership.Show();
}
}
}
Any help would be appreciated.
Thank you.
Use Application.Session.CurrentUser.AddressEntry.GetExchangeUser().GetMemberOfList() - it will return AddressEntries object that contains all DLs that the user is a member of.
Be prepared to handle nulls and errors.

Automatically map a Contact to an Account

I want to add a field to Accounts which shows the email domain for that account e.g. #BT.com. I then have a spreadsheet which lists all the Accounts and their email domains. What I want to do is when a new Contact is added to Dynamics that it checks the spreadsheet for the same email domain (obviously without the contacts name in the email) and then assigned the Contact to the Account linked to that domain. Any idea how I would do this. Thanks
Probably best chance would be to develop CRM plugin. Register your plugin to be invoked when on after contact is created or updated (so called post-event phase). And in your plugin update the parentaccountid property of the contact entity to point to account of your choice.
Code-wise it goes something like (disclaimer: not tested):
// IPluginExecutionContext context = null;
// IOrganizationService organizationService = null;
var contact = (Entity)context.InputParameters["Target"];
var email = organizationService.Retrieve("contact", contact.Id, new ColumnSet("emailaddress1")).GetAttributeValue<string>("emailaddress1");
string host;
try
{
var address = new MailAddress(email);
host = address.Host;
}
catch
{
return;
}
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
contact["parentaccountid"] = entities[0].ToEntityReference();
}
organizationService.Update(contact);
I took Ondrej's code and cleaned it up a bit, re-factored for pre-operation. I also updated the logic to only match active account records and moved the query inside the try/catch. I am unfamiliar with the MailAddress object, I personally would just use string mapping logic.
var target = (Entity)context.InputParameters["Target"];
try
{
string host = new MailAddress(target.emailaddress1).Host;
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
query.Criteria.AddCondition("statecode", ConditionOperator.Equals, 0); //Active records only
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
target["parentaccountid"] = entities[0].ToEntityReference();
}
}
catch
{
//Log error
}

Can Dynamics Crm PartyList store an emailaddress

I have fields in activity form for email. It contains "to, cc and bcc" fields that are all fields of the type PartyList
The question is: Can I only store entity values like contact or account or can I also just store a email address which is not associated to any contact or account in the system?
Here is a picture explaining what I'm trying to achieve
As per this article it appears that the answer is yes via the addressUsed field.
Entity email = _sdk.Retrieve("email", emailId, new ColumnSet("to"));
EntityCollection to = email.GetAttributeValue<EntityCollection>("to");
if (to != null)
{
to.Entities.ToList().ForEach(party =>
{
EntityReference partyId = party.GetAttributeValue<EntityReference>("partyid");
bool isDeleted = party.GetAttributeValue<bool>("ispartydeleted");
string addressUsed = party.GetAttributeValue<string>("addressused");
// Do something...
});
}

How to get client's contact details in lync sdk c#

Which Object has the client contact information like office,company,IM, etc,. in Lync SDK 2013? I want to know the user's(client's) location/address information.
User location/office information can be obtained from contact object as given below:
LyncClient lyncClient = LyncClient.GetClient();
Contact contact = lyncClient.ContactManager.GetContactByUri("sip:contact#organization.com");
String officeLocation = contact.GetContactInformation(ContactInformationType.Office).ToString();
More information can be obtained using Contact information types Personal Note, Company, Location, Department etc.
In addition to Kannan's answer, getting the phone numbers from a contact is different and requires more work. Here's how you do it:
LyncClient lyncClient = LyncClient.GetClient();
Contact contact = lyncClient.ContactManager.GetContactByUri("sip:contact#organization.com");
List<object> endPoints = new List<object>();
var telephoneNumber = (List<object>)contact.GetContactInformation(ContactInformationType.ContactEndpoints);
endPoints = telephoneNumber.Where<object>(N => ((ContactEndpoint)N).Type == ContactEndpointType.HomePhone || ((ContactEndpoint)N).Type == ContactEndpointType.MobilePhone || ((ContactEndpoint)N).Type == ContactEndpointType.OtherPhone || ((ContactEndpoint)N).Type == ContactEndpointType.WorkPhone).ToList<object>();
foreach (var endPoint in endPoints)
{
//((ContactEndpoint)endPoint).DisplayName.ToString(); //This is the phone number in string format
}

save smtp sent mail in outlook sent folder using outlook add in

I would like to send smtp send mail from outlook-addin that mail save into outlook sent folder
Note: Save mail item in sent folder from address must be which i specified smtp from address not outlook login username.
public bool SendEMail()
{
MailMessage mailNew = new MailMessage();
var smtp = new SmtpClient("SmtpServer")
{
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network
};
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("UserName", "password");
smtp.EnableSsl = false;
smtp.Credentials = credentials;
MailAddress mailFrom = new MailAddress("clark#gmail.com");
mailNew.From = mailFrom;
mailNew.To.Add("someone#gmail.com");
mailNew.Subject = Subject;
mailNew.IsBodyHtml = Html;
mailNew.Body = Body;
smtp.Send(mailNew);
return true;
}
thanks in advance
You will need to create a sent item in the Sent Items folder and set all the relevant properties using MailItem.PropertyAccessor. Note that PropertyAccessor will not let you set some sender related properties. In Outlook 2010 or higher you can set the MailItem.Sender property.
Also note that the sent flag cannot be changed after the message is saved, so to create a sent item using OOM, you will need to create a post item, then change its MessageClass property to "IPM.Note".
Also note that ReceivedTime and SentOn properties are reset by OOM every time the message is saved.
If using Redemption is an option (I am its author), you can do something like the following (off the top of my head):
Redemption.RDOSession session = new Redemption.RDOSession();
session.MAPIOBJECT = Application.Session.MAPIOBJECT;
Redemption.RDOMail message = session.GetDefaultFolder(rdoDefaultFolders.olFolderSentMail).Items.Add("IPM.Note");
message.Sent = true;
message.Subject = "test";
message.Body = "fake sent message";
message.Recipients.AddEx("The Recipient", "recipient#domain.com", "SMTP", olTo);
string senderEntryID = session.AddressBook.CreateOneOffEntryID("Some Name", "SMTP", "user#domain.com", false, true);
addressEntry = session.AddressBook.GetAddressEntryFromID(senderEntryID);
message.Sender = addressEntry;
message.SentOnBehalfOf = addressEntry;
message.Save();

Resources