none
c++ Programmatically add certificate to Outlook contacts RRS feed

  • Question

  • I am developing a  Add-in of MS Outlook using c plus plus, and I am using Microsoft.Office.Interop.Outlook, I have a problem with using get_Usercertificate() function.

    The following code is used by my project:

    HRESULT hr=m_spApp->GetNamespace(bst,&spNamespace);  
    spNamespace->GetDefaultFolder(olFolderContacts,&spMAPIFold);
    hr=spMAPIFold->get_Items(&spItems);
    spItems->Item(index,&pDisp);
    spContactItem=pDisp;
    hr=spContactItem->get_UserCertificate(&cer_bstr);

    who can tell me why the cer_bstr not  get any information about certificate.

    I find that the cer_bstr's length is always zero. I‘m sure that the contactItem contain a certificate.

    Thanks !

    • Moved by Cindy Meister MVP Thursday, October 25, 2012 9:02 PM not using VSTO technology (From:Visual Studio Tools for Office)
    Thursday, October 25, 2012 12:25 PM

Answers

  • Your code is setting a certificate, not retrieving it.

    Even so, you are passing a string. For a PT_MV_BINARY property such as PR_USER_X509_CERTIFICATE, you will need a variant array of variant arrays each containing bytes as elements.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, November 5, 2012 7:05 AM

All replies

  • Hi New programmer 007,

    Thank you for posting in the MSDN Forum.

    I'll involve some experts who are more familiar with your issue. This may take some time.

    Thank you for your patient and understanding.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 26, 2012 6:40 AM
    Moderator
  • Can you see the certificate if you look at the contact with OutlookSpy (click IMessage)?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, October 26, 2012 2:46 PM
  • Thank you for your reply!

    I can not quite understand what you mean. But I still do you say, then I find a property tag (PR_USER_X509_CERTIFICATE) relate to it. I find Usercertificate property has been deprecated, so I think the get_UserCertificate should also be deprecated. What should I do can import a digital certificate into contactitem?

     

    Sunday, October 28, 2012 8:47 AM
  • Can you access it using ContactItem.PropertyAccessor?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Sunday, October 28, 2012 4:14 PM
  • NO! I was the first to do this and  do not know how to use it !

    Can you provide a simple example with c++?

    Thank you!

    Monday, October 29, 2012 12:28 AM
  • What is your existing code and what happens when you run it?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Tuesday, October 30, 2012 6:37 AM
  • I am developing a project.  I was asked to check whether ConnactItem contains certificate . So I use get_UserCertificate to complete it. if cer_bstr's length (cer_bstr is outputed by this function) is zero, I would think this ConnactItem not contains certificate and then I use the put_UserCertificate function to import certificate .

    Now I am learning to use this function, so I only use this code and when I run it I found the get_UserCertificate function is not work (or the value of cer_bstr always remains unchanged ) . I hope to know  how to handle certificate( For example ,import or  export certificate to Outlook). 

    Tuesday, October 30, 2012 12:52 PM
  • If you see the PR_USER_X509_CERTIFICATE property in OutlookSpy (click IMessage) or MFCMAPI, you can access it using ContactItem.PropertyAccessor.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Tuesday, October 30, 2012 4:31 PM
  • Yes, I am trying to do this. The follow code:

    unsigned char* cer_char;
    cer_char=new unsigned char[Cer_Max_Len];
    IntPtr vPtr(cer_char);

    Marshal::Copy(rawdata,0,vPtr,cer_len);
    System::String^ cer_str;

    cer_str=vPtr.ToString();
    char* cert_char=(char*)Marshal::StringToHGlobalAnsi(cer_str).ToPointer();
    cert.bstrVal=_com_util::ConvertStringToBSTR(cert_char);


    BSTR SetPro;
    SetPro=::SysAllocString(L"http://schemas.microsoft.com/mapi/proptag/0x3A701102");


    HRESULT hr=m_spApp->GetNamespace(bst,&spNamespace);  
    if (FAILED(hr))
    MessageBox(NULL,L"GetNamespace error",L"Error",MB_OK);
    SysFreeString(bst);

    spNamespace->GetDefaultFolder(olFolderContacts,&spMAPIFold);
    hr=spMAPIFold->get_Items(&spItems);
    if (FAILED(hr))
    MessageBox(NULL,L"Items failed",L"Error",MB_OK);

    spItems->Item(index,&pDisp);
    spContactItem=pDisp;

    spContactItem->get_PropertyAccessor(&spPa);
    spPa->SetProperty(SetPro,cert);

    But when I test it by using Outlook to send encrypted E-mail I found Outlook provide a warning:the certificate does not exist or invalid .Can you tell me what cause it?

    Thank you!

    Thursday, November 1, 2012 3:03 AM
  • Your code is setting a certificate, not retrieving it.

    Even so, you are passing a string. For a PT_MV_BINARY property such as PR_USER_X509_CERTIFICATE, you will need a variant array of variant arrays each containing bytes as elements.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, November 5, 2012 7:05 AM
  •    yes, I  do it ,

    VARIANT test=spSet.setpro();
    HRESULT hr=spPa->SetProperty(SetPro,test);

    The spSet is a class and the setpro function is used to get rawdata of certificate.

    But I found  the value of hr is 0x80020005 . Can you tell me which parameter's type dismatch. I've done a lot of attempts but without success。

    Monday, November 12, 2012 3:30 PM
  • The error is DISP_E_TYPEMISMATCH.

    I can see that setpro() is a function, but what exactly does it return?  A variant array of variant arrays?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, November 12, 2012 3:35 PM
  • X509Certificate2^ x509=gcnew X509Certificate2("C:\\shiyan.cer");
    array<Byte>^rawdata=x509->RawData;

            int cer_len = rawdata->Length;
    int* cer_data;
    cer_data=new int[cer_len];

            IntPtr vPtr(cer_char);
    Marshal::Copy(rawdata,0,vPtr,cer_len);

           VARIANT test;

           test.vt=VT_BYREF|VT_INT;

           test.pintVal=cer_data;


    Tuesday, November 13, 2012 12:38 PM
  • Hi,

    With Outlook Object Model, this property is not functional and is not intended for use. Refer: http://msdn.microsoft.com/en-us/library/office/aa221768(v=office.11).aspx. This property cannot be used with Outlook 2003 OOM, but can be tried from Outlook 2007 OOM.

    In general, UserCertificate property returns the PR_USER_CERTIFICATE property, but the contact's certificate information is saved in the PR_USER_X509_CERTIFICATE property. If we want to make use of OOM, then you can try the PropertyAccessor Object like: Outlook.ContactItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3a710003"). So first make sure that if you notice PR_USER_X509_CERTIFICATE property in MFCMAPI utility, then they can access it using ContactItem.PropertyAccessor()

    Regards,

    Shailesh G. Rajput
    ______________________________________________
    Microsoft Developer Support - Messaging & Collaboration

    Thursday, November 29, 2012 11:19 PM