Defeat "Program Attempts to Access Email" with SendMessage

I am writing a C # application that uses Office Outlook Interop (2010; version 14) to access email data through MAPI.

So far, I have had to manually and repeatedly click “Allow” in Outlook “ The program is trying to access information about email addresses stored in Outlook ” (screenshot in http://i.stack.imgur.com/gj8to.png ) .

I tried to write a method to automatically push, but to no avail. A click occurs, but Outlook Interop throws the following exception during the next read operation:

HRESULT exception: 0x80004004 (E_ABORT)

Here is an excerpt from the code that SendMessage uses to click the Allow button:

private const Int32 BM_CLICK = 0x00F5;
private const Int32 GWL_STYLE = -16;
private const Int32 CB_SETCURSEL = 0x14E;
private const Int32 WM_LBUTTONDOWN = 0x201;
private const Int32 WM_LBUTTONUP = 0x202;

private const string dialogClass = "#32770";
private const string dialogTitle = "Microsoft Outlook";
const string tickBoxTitle = @"&Allow access for";
const string tickBoxClass = @"Button";

private const int ThreadSleepTime = 1000;
private const int ThreadUiSleepTime = 20;

IntPtr popupHandle;
IntPtr tickHandle = IntPtr.Zero;
IntPtr comboHandle = IntPtr.Zero;
IntPtr allowHandle = IntPtr.Zero;
while ((popupHandle = FindWindow(dialogClass, dialogTitle)) != IntPtr.Zero)
{
    // Get object handles
    tickHandle = FindWindowEx(popupHandle, tickHandle, tickBoxClass, tickBoxTitle);
    comboHandle = FindWindowEx(popupHandle, tickHandle, null, null);
    allowHandle = FindWindowEx(popupHandle, comboHandle, null, null);

    // Click on tickbox until the combobox is enabled
    while ((GetWindowLong(comboHandle, GWL_STYLE) & 0x8000000) != 0)
    {
        SendMessage(tickHandle, BM_CLICK, new IntPtr(1), IntPtr.Zero);
        System.Threading.Thread.Sleep(ThreadUiSleepTime);
    }

    // Set dropdown box selection index to 3rd row
    SendMessage(comboHandle, CB_SETCURSEL, 3, 0);
    System.Threading.Thread.Sleep(ThreadUiSleepTime);

    // Click Allow button
    //SendMessage(allowHandle, BM_CLICK, new IntPtr(1), IntPtr.Zero);

    SendMessage(allowHandle, WM_LBUTTONDOWN, IntPtr.Zero, new IntPtr(MakeLParam(5, 5)));
    System.Threading.Thread.Sleep(ThreadUiSleepTime); 
    SendMessage(allowHandle, WM_LBUTTONUP, IntPtr.Zero, new IntPtr(MakeLParam(5, 5)));
}

Why is SendMessage not working and how can I do this?

+2
source share
2 answers

You must use an EMAPI that does not display these warnings.
The Redemption library provides a convenient shell.

+2
source

There should be no way to override due to spam abuse.

However, using a different thread to search and click a button works.

Note that SendMessage does not work for mouse buttons. You must use the accessibility API or WM_COMMAND.

, , , .

+1

All Articles