I’m trying to read out the Internet formatted address from an Exchange connected Outlook. I read all contacts from the Outlook Contacts, i.e. not from the Global Address Book (GAB), and the problem is that for all users that are stored in Contacts from the Exchange GAB I’ve only managed to read out the X.500 formatted address which is not useful in this case. For all manually added contacts that are not in the domain of the Exchange server, the Internet address is exported as expected.
Basically I’ve used the following code snippet to enumerate the Contacts:
static void Main(string[] args)
{
var outlookApplication = new Application();
NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
MAPIFolder contacts = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
for (int i = 1; i < contacts.Items.Count + 1; i++)
{
try
{
ContactItem contact = (ContactItem)contacts.Items[i];
Console.WriteLine(contact.FullName);
Console.WriteLine(contact.Email1Address);
Console.WriteLine(contact.Email2Address);
Console.WriteLine(contact.Email3Address);
Console.WriteLine();
}
catch (System.Exception e) { }
}
Console.Read();
}
Is there any way to extract the Internet address instead of the X.500?
You need to convert from ContactItem to AddressEntry - one email address at a time.
To do this, you need to access the AddressEntry via the Recipient object model. The only way to retrieve the actual Recipient EntryID is by leveraging the PropertyAccessor of the ContactItem.
const string Email1EntryIdPropertyAccessor = "http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80850102";
string address = string.Empty;
Outlook.Folder folder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) as Outlook.Folder;
foreach (var contact in folder.Items.Cast<Outlook.ContactItem>().Where(c=>!string.IsNullOrEmpty(c.Email1EntryID)))
{
Outlook.PropertyAccessor propertyAccessor = contact.PropertyAccessor;
object rawPropertyValue = propertyAccessor.GetProperty(Email1EntryIdPropertyAccessor);
string recipientEntryID = propertyAccessor.BinaryToString(rawPropertyValue);
Outlook.Recipient recipient = this.Application.Session.GetRecipientFromID(recipientEntryID);
if (recipient != null && recipient.Resolve() && recipient.AddressEntry != null)
address = recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
}
Related
I try Mictosoft example to create and and contact to exchange server using EWS and strangly mark as invalid while try using fax number for sending using Windows Fax and Scan (wfs.exe).
So , Does anybody know how should I set fax number in EWS that can be usable for sending fax in fax and scan?
After a lot of exprementing and invaestigating using OutlookSpy I found out I should add a # before number to be recognizable by Windows Fax and Scan. This is my final code.
public static ExchangeService GetService(string emailUser = defaultUser, string pass = defaultPass)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
//service.AutodiscoverUrl(emailUser, RedirectionUrlValidationCallback);
service.Url = new Uri("https://xs3.domain.net/EWS/Exchange.asmx");
service.Credentials = new WebCredentials(emailUser, pass);
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
return service;
}
public static bool IsValidEmail(string email)
{
try
{
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
public static void AddOrUpdateContact(ExchangeService service, string id, Dictionary<string, string> dataRecord, string targetFolder = defaultCotactsFolder, bool faxOnly = false)
{
Folder parentFolder;
FindFoldersResults folderItems = service.FindFolders(
WellKnownFolderName.Contacts,//parent folder
new SearchFilter.IsEqualTo(FolderSchema.DisplayName, targetFolder),
new FolderView(1)// return first 1 records
);
if (folderItems != null && folderItems.Count() > 0)
{//folder found!
parentFolder = folderItems.First() as Folder;
}
else
{//folder NOT found!
return;
}
ExtendedPropertyDefinition CustomProperty = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "BusinessFax", MapiPropertyType.String);
FindItemsResults<Item> contactItems = service.FindItems(
parentFolder.Id,
new SearchFilter.IsEqualTo(ContactSchema.NickName, id),
new ItemView(1)
);
bool doesExist = contactItems != null && contactItems.Count() > 0;
Contact contact;
if (doesExist)
{//contact found!
contact = contactItems.First() as Contact;
}
else
{
// Create the contact.
contact = new Contact(service);
}
// Specify the name and how the contact should be filled.
contact.CompanyName = name;
contact.NickName = id;
contact.PhoneNumbers[PhoneNumberKey.BusinessFax] = "#"+dataRecord["COD_FAX_SUPID"];
contact.FileAsMapping = FileAsMapping.Company;
if (!faxOnly)
{
// Specify the business, home, and car phone numbers.
contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = dataRecord["COD_TEL_SUPID"];
if (IsValidEmail(dataRecord["EMAIL_SUPID"]))
{
// Specify two email addresses.
contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress(dataRecord["EMAIL_SUPID"]);
}
contact.PhysicalAddresses[PhysicalAddressKey.Home] = new PhysicalAddressEntry()
{
Street = dataRecord["DES_STREET_SUPID"],
City = dataRecord["DES_TOWN_SUPID"],
CountryOrRegion = dataRecord["DES_COUNTRY_SUPID"],
PostalCode = dataRecord["COD_POST_SUPID"] + (String.IsNullOrWhiteSpace(dataRecord["NUM_BOX_SUPID"]) ? "" : $", PostBox: {dataRecord["NUM_BOX_SUPID"]}"),
}
}
if (doesExist)
{ //contact found!
contact.Update(ConflictResolutionMode.AlwaysOverwrite);
}
else
{
//save new!
contact.Save(parentFolder.Id);
}
}
Usage Example:
ExchangeService service = ContatctUpdaterLib.ContatcUpdater.GetService("USER#DONAMAIN.COM","123456");
//GetUserData return user data record as a Dictionary<string,string>
var data = GetUserData();
AddOrUpdateContact(service, dataRecord["COD_SUP_SUPID"], dataRecord, "Contacts(Fax Only)" ,true);
I have an application that creates a .vcf file after you input data. it stores storage provided by the phone, I want to click on a list view item and get the vcf file and share it via Email, SMS, WhatsApp, Skype etc how do I implement this in IOS and Android.
Thank you
I have got the answer of creating .vcf file which is given below. and for share that file follow this link : https://github.com/adamped/ShareDialog
private void Share_Clicked(object sender, EventArgs e)
{
try
{
var _btn = sender as Button;
var record = _btn.BindingContext as Contact;
int tempcontactID = record.ContactID;
if (record.CardFrontImage == null)
{
record.CardImage = record.CardBackImage;
}
else
{
record.CardImage = record.CardFrontImage;
}
string baseimage = Convert.ToBase64String(record.CardImage);
var vcf = new StringBuilder(); //vcf code start
vcf.AppendLine("BEGIN:VCARD");
vcf.AppendLine("VERSION:3.0");
vcf.AppendLine($"N:{record.ContactName};{string.Empty}; ;;");
vcf.AppendLine($"FN:{record.ContactName}");
vcf.AppendLine($"ORG:{record.CompanyName}");
vcf.AppendLine($"TITLE:{record.Designation}");
vcf.AppendLine($"PHOTO;ENCODING=BASE64;TYPE=PNG:{baseimage}");
vcf.AppendLine($"TEL;TYPE=work,voice;VALUE=uri:tel:{record.PhoneNumber}");
vcf.AppendLine("END:VCARD");
string fileName = Path.Combine("/storage/emulated/0/Android/data/com.Gamma.GammaNetworkingApp/files/", record.ContactID + record.ContactName + ".vcf");
using (var writer = new StreamWriter(fileName))
{
writer.Write(vcf.ToString());
}
string text = File.ReadAllText(fileName);
bool doesExist = File.Exists(fileName);
if (doesExist == true)
{
var share = DependencyService.Get<IShare>();
share.Show("Contact share", record.ContactName, fileName);
}
}
catch (Exception ex)
{
string test = ex.ToString();
Navigation.PushAsync(new HomePage());
}
}
I want to develop Office Outlook Plug-in to upload email message attachments if it exceeds for example 20 MB to OneDrive or SharePoint Online and add links for attachments to the message body while sending.
I need some guide to start, just show me the way and I'll walk it.
Update:
I've developed a part but once code reach clientContext.ExecuteQuery(); outlook crash.
private void ApplicationOnItemSend(object item, ref bool cancel)
{
var mailItem = item as Outlook.MailItem;
//Upload to SharePoint
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
if (IsAttachmentExceedLimit(attachment))
{
byte[] attachmentData = null;
// this is a standard attached object
attachmentData = attachment.PropertyAccessor.GetProperty(PrAttachDataBin) as byte[];
MemoryStream theMemStream = new MemoryStream();
theMemStream.Write(attachmentData, 0, attachmentData.Length);
theMemStream.Position = 0;
try
{
//Upload attachment to SharePoint Online
bool overwrite = Settings.Default.Overwrite;
using (ClientContext clientContext = ClaimClientContext.GetAuthenticatedContext(Settings.Default.SharePointSiteUrl))
{
var web = clientContext.Web;
var list = web.Lists.GetByTitle(Settings.Default.SharePointFolder);
var newFileFromComputer = new FileCreationInformation
{
Content = attachmentData,
Url = attachment.FileName
};
var uploadedFile = list.RootFolder.Files.Add(newFileFromComputer);
clientContext.Load(uploadedFile);
clientContext.RequestTimeout = 360000000;
clientContext.ExecuteQuery();
}
//Add Link for attachment
mailItem.HTMLBody += Settings.Default.SharePointSiteUrl + Settings.Default.SharePointFolder +
attachment.FileName + Environment.NewLine;
//Delete attachement from mail message
mailItem.Attachments.Remove(attachment.Index);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Note:
I don't have any experience in VSTO.
The problem I want to solve:
Get all the contacts info like name and Mobile phone and write it into file and save in ISO.
How to use SearchAsync if I want to search available contacts in the phone?
How to iterate the return-results and write to file one by one of the contact into a file?
Here's the code I have:
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
Contacts contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(displayName,FilterKind.DisplayName,null);
//search for all contacts
contacts.SearchAsync(string.Empty, FilterKind.None, null);
}
Update:
The below code throw NullException error if the PhoneNumber is Empty. Why?
How to get all the possibile phone number other than result.PhoneNumbers.FirstOrDefault().ToString();
Same question for EmailAddresses
Using this to search all contacts in the phone:
contacts.SearchAsync(searchterm, FilterKind.None, null);
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
int intTTL = e.Results.Count();
if (intTTL != 0)
{
MessageBox.Show(intTTL.ToString());
foreach (var result in e.Results)
{
string strTTL;
string strName = result.DisplayName;
string MobileNo = result.PhoneNumbers.FirstOrDefault().ToString();
strTTL = strName + "," + MobileNo;
MessageBox.Show(strTTL);
}
else
{
MessageBox.Show("You have not entered any contact info at all.");
}
}
Have a look at this article to see how to get the Contacts:
To iterate:
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
foreach(var contact in e.Results)
{
// write to Isolated storage
}
}
See this MSDN page for writing files in isolated storage
I need to read records containing name and email from a file or database and add them to an existing Oulook distribution list (from the private contacts, not from the GAL).
I just saw examples of reading from OL using LINQ to DASL which I have working for mail and appointments, but I can't figure out how to list the contents of a dist list:
private static void GetContacts()
{
Outlook.Application app = new Outlook.Application();
Outlook.Folder folder = (Outlook.Folder)app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
var distLists = from item in folder.Items.AsQueryable<MyDistList>()
where item.DLName == "My Dist List"
select item.Item;
var builder = new StringBuilder();
foreach (var list in distLists)
{
builder.AppendLine(list.DLName);
foreach (var item in list.Members)
{
// can't figure out how to iterate through the members here
// compiler says Object doesn't have GeNumerator...
}
}
Console.WriteLine(builder.ToString());
Console.ReadLine();
}
Once I can read the members I need to be able to add new ones which is even more trick. Any help would be appreciated.
Turns out it is easy enough. I was simply missing the call to Resolve as I thought that was only if you were resolving against the GAL:
Outlook.Recipient rcp = app.Session.CreateRecipient("Smith, John<j.smith#test.com>");
rcp.Resolve();
list.AddMember(rcp);
list.Save();
And I can create an iterator that uses the distList.GetMember method:
// Wrap DistListItem.GetMembers() as an iterator
public static class DistListItemExtensions
{
public static IEnumerable<Outlook.Recipient> Recipients(this Outlook.DistListItem distributionList)
{
for (int i = 1; i <= distributionList.MemberCount; i++)
{
yield return distributionList.GetMember(i);
}
}
}