VSTO outlook mailItem Application_ItemSend to catach a contactItem from a recipient of Active Directory












0















how to get ContactItem from Recipients property by Active directory. I had trid the code and get the ContactItem seccsussful when the Recipients is saved in local contact. how to get the ContactItem when a Recipients was only exists only on Active directory.



public void Application_ItemSend(object mail, ref bool Cancel)
{



        Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
for (int i = 1; i < mail.Recipients.Count + 1; i++)
{
Outlook.Recipient r = mail.Recipients.Item(i);
if (!r.Resolved) r.Resolve();
if (r.Resolved)
{
Outlook.ContactItem ci = (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);
if (ci != null)
{
//to get the Department of Recipient
string DepartmentName = ci.Department;
}
}
}

}









share|improve this question























  • the ci ContactItem is already null. I don`t know where is wrong.

    – SuperDeve
    Nov 26 '18 at 5:39
















0















how to get ContactItem from Recipients property by Active directory. I had trid the code and get the ContactItem seccsussful when the Recipients is saved in local contact. how to get the ContactItem when a Recipients was only exists only on Active directory.



public void Application_ItemSend(object mail, ref bool Cancel)
{



        Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
for (int i = 1; i < mail.Recipients.Count + 1; i++)
{
Outlook.Recipient r = mail.Recipients.Item(i);
if (!r.Resolved) r.Resolve();
if (r.Resolved)
{
Outlook.ContactItem ci = (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);
if (ci != null)
{
//to get the Department of Recipient
string DepartmentName = ci.Department;
}
}
}

}









share|improve this question























  • the ci ContactItem is already null. I don`t know where is wrong.

    – SuperDeve
    Nov 26 '18 at 5:39














0












0








0








how to get ContactItem from Recipients property by Active directory. I had trid the code and get the ContactItem seccsussful when the Recipients is saved in local contact. how to get the ContactItem when a Recipients was only exists only on Active directory.



public void Application_ItemSend(object mail, ref bool Cancel)
{



        Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
for (int i = 1; i < mail.Recipients.Count + 1; i++)
{
Outlook.Recipient r = mail.Recipients.Item(i);
if (!r.Resolved) r.Resolve();
if (r.Resolved)
{
Outlook.ContactItem ci = (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);
if (ci != null)
{
//to get the Department of Recipient
string DepartmentName = ci.Department;
}
}
}

}









share|improve this question














how to get ContactItem from Recipients property by Active directory. I had trid the code and get the ContactItem seccsussful when the Recipients is saved in local contact. how to get the ContactItem when a Recipients was only exists only on Active directory.



public void Application_ItemSend(object mail, ref bool Cancel)
{



        Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
for (int i = 1; i < mail.Recipients.Count + 1; i++)
{
Outlook.Recipient r = mail.Recipients.Item(i);
if (!r.Resolved) r.Resolve();
if (r.Resolved)
{
Outlook.ContactItem ci = (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);
if (ci != null)
{
//to get the Department of Recipient
string DepartmentName = ci.Department;
}
}
}

}






outlook vsto contactitem






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 5:21









SuperDeveSuperDeve

113




113













  • the ci ContactItem is already null. I don`t know where is wrong.

    – SuperDeve
    Nov 26 '18 at 5:39



















  • the ci ContactItem is already null. I don`t know where is wrong.

    – SuperDeve
    Nov 26 '18 at 5:39

















the ci ContactItem is already null. I don`t know where is wrong.

– SuperDeve
Nov 26 '18 at 5:39





the ci ContactItem is already null. I don`t know where is wrong.

– SuperDeve
Nov 26 '18 at 5:39












2 Answers
2






active

oldest

votes


















0














You could refer to the below code:



bool resolved;
Microsoft.Office.Interop.Outlook.Application olApplication = new Microsoft.Office.Interop.Outlook.Application();

// get nameSpace and logon.
Microsoft.Office.Interop.Outlook.NameSpace olNameSpace = olApplication.GetNamespace("mapi");
olNameSpace.Logon("Outlook", "", false, true);

// get the Calender items
Microsoft.Office.Interop.Outlook.MAPIFolder _olCalender = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

// Get the Items (Appointments) collection from the Calendar folder.
Microsoft.Office.Interop.Outlook.Items oItems = _olCalender.Items;



foreach (object o in oItems)
{

if (o is Microsoft.Office.Interop.Outlook.AppointmentItem)
{
Microsoft.Office.Interop.Outlook.Recipients recipients = ((Microsoft.Office.Interop.Outlook.AppointmentItem)o).Recipients;
foreach (Microsoft.Office.Interop.Outlook.Recipient rec in recipients)
{
resolved = rec.Resolve();
if (resolved)
{
Microsoft.Office.Interop.Outlook.ContactItem contactItem = rec.AddressEntry.GetContact();

}
}

}
}


For more information, Please refer to this link:



C# Outlook get CompanyName property from Recipient






share|improve this answer
























  • Thank you for your comment . I had trid it when you add the comment,but it wasn`t used when the recipe exsits only on Active Directory.

    – SuperDeve
    Nov 26 '18 at 6:36





















0














just change the code like the folowing:



Outlook.ContactItem ci =
(fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);



change to
ExchangeUser contactItem = recip.AddressEntry.GetExchangeUser();






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475124%2fvsto-outlook-mailitem-application-itemsend-to-catach-a-contactitem-from-a-recipi%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could refer to the below code:



    bool resolved;
    Microsoft.Office.Interop.Outlook.Application olApplication = new Microsoft.Office.Interop.Outlook.Application();

    // get nameSpace and logon.
    Microsoft.Office.Interop.Outlook.NameSpace olNameSpace = olApplication.GetNamespace("mapi");
    olNameSpace.Logon("Outlook", "", false, true);

    // get the Calender items
    Microsoft.Office.Interop.Outlook.MAPIFolder _olCalender = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

    // Get the Items (Appointments) collection from the Calendar folder.
    Microsoft.Office.Interop.Outlook.Items oItems = _olCalender.Items;



    foreach (object o in oItems)
    {

    if (o is Microsoft.Office.Interop.Outlook.AppointmentItem)
    {
    Microsoft.Office.Interop.Outlook.Recipients recipients = ((Microsoft.Office.Interop.Outlook.AppointmentItem)o).Recipients;
    foreach (Microsoft.Office.Interop.Outlook.Recipient rec in recipients)
    {
    resolved = rec.Resolve();
    if (resolved)
    {
    Microsoft.Office.Interop.Outlook.ContactItem contactItem = rec.AddressEntry.GetContact();

    }
    }

    }
    }


    For more information, Please refer to this link:



    C# Outlook get CompanyName property from Recipient






    share|improve this answer
























    • Thank you for your comment . I had trid it when you add the comment,but it wasn`t used when the recipe exsits only on Active Directory.

      – SuperDeve
      Nov 26 '18 at 6:36


















    0














    You could refer to the below code:



    bool resolved;
    Microsoft.Office.Interop.Outlook.Application olApplication = new Microsoft.Office.Interop.Outlook.Application();

    // get nameSpace and logon.
    Microsoft.Office.Interop.Outlook.NameSpace olNameSpace = olApplication.GetNamespace("mapi");
    olNameSpace.Logon("Outlook", "", false, true);

    // get the Calender items
    Microsoft.Office.Interop.Outlook.MAPIFolder _olCalender = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

    // Get the Items (Appointments) collection from the Calendar folder.
    Microsoft.Office.Interop.Outlook.Items oItems = _olCalender.Items;



    foreach (object o in oItems)
    {

    if (o is Microsoft.Office.Interop.Outlook.AppointmentItem)
    {
    Microsoft.Office.Interop.Outlook.Recipients recipients = ((Microsoft.Office.Interop.Outlook.AppointmentItem)o).Recipients;
    foreach (Microsoft.Office.Interop.Outlook.Recipient rec in recipients)
    {
    resolved = rec.Resolve();
    if (resolved)
    {
    Microsoft.Office.Interop.Outlook.ContactItem contactItem = rec.AddressEntry.GetContact();

    }
    }

    }
    }


    For more information, Please refer to this link:



    C# Outlook get CompanyName property from Recipient






    share|improve this answer
























    • Thank you for your comment . I had trid it when you add the comment,but it wasn`t used when the recipe exsits only on Active Directory.

      – SuperDeve
      Nov 26 '18 at 6:36
















    0












    0








    0







    You could refer to the below code:



    bool resolved;
    Microsoft.Office.Interop.Outlook.Application olApplication = new Microsoft.Office.Interop.Outlook.Application();

    // get nameSpace and logon.
    Microsoft.Office.Interop.Outlook.NameSpace olNameSpace = olApplication.GetNamespace("mapi");
    olNameSpace.Logon("Outlook", "", false, true);

    // get the Calender items
    Microsoft.Office.Interop.Outlook.MAPIFolder _olCalender = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

    // Get the Items (Appointments) collection from the Calendar folder.
    Microsoft.Office.Interop.Outlook.Items oItems = _olCalender.Items;



    foreach (object o in oItems)
    {

    if (o is Microsoft.Office.Interop.Outlook.AppointmentItem)
    {
    Microsoft.Office.Interop.Outlook.Recipients recipients = ((Microsoft.Office.Interop.Outlook.AppointmentItem)o).Recipients;
    foreach (Microsoft.Office.Interop.Outlook.Recipient rec in recipients)
    {
    resolved = rec.Resolve();
    if (resolved)
    {
    Microsoft.Office.Interop.Outlook.ContactItem contactItem = rec.AddressEntry.GetContact();

    }
    }

    }
    }


    For more information, Please refer to this link:



    C# Outlook get CompanyName property from Recipient






    share|improve this answer













    You could refer to the below code:



    bool resolved;
    Microsoft.Office.Interop.Outlook.Application olApplication = new Microsoft.Office.Interop.Outlook.Application();

    // get nameSpace and logon.
    Microsoft.Office.Interop.Outlook.NameSpace olNameSpace = olApplication.GetNamespace("mapi");
    olNameSpace.Logon("Outlook", "", false, true);

    // get the Calender items
    Microsoft.Office.Interop.Outlook.MAPIFolder _olCalender = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

    // Get the Items (Appointments) collection from the Calendar folder.
    Microsoft.Office.Interop.Outlook.Items oItems = _olCalender.Items;



    foreach (object o in oItems)
    {

    if (o is Microsoft.Office.Interop.Outlook.AppointmentItem)
    {
    Microsoft.Office.Interop.Outlook.Recipients recipients = ((Microsoft.Office.Interop.Outlook.AppointmentItem)o).Recipients;
    foreach (Microsoft.Office.Interop.Outlook.Recipient rec in recipients)
    {
    resolved = rec.Resolve();
    if (resolved)
    {
    Microsoft.Office.Interop.Outlook.ContactItem contactItem = rec.AddressEntry.GetContact();

    }
    }

    }
    }


    For more information, Please refer to this link:



    C# Outlook get CompanyName property from Recipient







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 26 '18 at 6:25









    Alina LiAlina Li

    633125




    633125













    • Thank you for your comment . I had trid it when you add the comment,but it wasn`t used when the recipe exsits only on Active Directory.

      – SuperDeve
      Nov 26 '18 at 6:36





















    • Thank you for your comment . I had trid it when you add the comment,but it wasn`t used when the recipe exsits only on Active Directory.

      – SuperDeve
      Nov 26 '18 at 6:36



















    Thank you for your comment . I had trid it when you add the comment,but it wasn`t used when the recipe exsits only on Active Directory.

    – SuperDeve
    Nov 26 '18 at 6:36







    Thank you for your comment . I had trid it when you add the comment,but it wasn`t used when the recipe exsits only on Active Directory.

    – SuperDeve
    Nov 26 '18 at 6:36















    0














    just change the code like the folowing:



    Outlook.ContactItem ci =
    (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);



    change to
    ExchangeUser contactItem = recip.AddressEntry.GetExchangeUser();






    share|improve this answer




























      0














      just change the code like the folowing:



      Outlook.ContactItem ci =
      (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);



      change to
      ExchangeUser contactItem = recip.AddressEntry.GetExchangeUser();






      share|improve this answer


























        0












        0








        0







        just change the code like the folowing:



        Outlook.ContactItem ci =
        (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);



        change to
        ExchangeUser contactItem = recip.AddressEntry.GetExchangeUser();






        share|improve this answer













        just change the code like the folowing:



        Outlook.ContactItem ci =
        (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);



        change to
        ExchangeUser contactItem = recip.AddressEntry.GetExchangeUser();







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 7:14









        SuperDeveSuperDeve

        113




        113






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475124%2fvsto-outlook-mailitem-application-itemsend-to-catach-a-contactitem-from-a-recipi%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga