Friday, August 3, 2012

How to get around, bypass or defeat "A program is trying to automatically send email on your behalf" in Outlook 2010

If you've encountered this error like I have, you know it is a pain in the ass. You're writing code and trying to test it and you get blocked by Outlook security. Today I found a good solution that should be more widely available on the internet, then it is. These two registry keys disable that functionality. A C sharp code example is below and what I used to send emails in a console app. It loops and send a new incrementing subject email every 5 seconds. The code is from the internet and other people, that I've tweaked, same as the reg hacks. I have not tried this with some of the other programmatic ways to interact with Office, nor with other versions of Office. This may also work or affect the "A program is trying to access e-mail addresses you have stored in Outlook" as well.


[HKEY_CURRENT_USER\Software\Policies\Microsoft\Security] "CheckAdminSettings"=dword:00000002
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\14.0\Outlook\Security] "ObjectModelGuard"=dword:00000002 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;

/*

 Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\Software\Policies\Microsoft\Security] "CheckAdminSettings"=dword:00000002
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\14.0\Outlook\Security] "ObjectModelGuard"=dword:00000002 
  
  
  
 */

namespace LateBindingTest
{
    class OutlookEmailerLateBinding
    {

        private object oApp;
        private object oNameSpace;
        private object oOutboxFolder;
        public static int messageNumber = 0;

        public OutlookEmailerLateBinding()
        {
            int loopNumber = 0;
            Type outlook_app_type;
            object[] parameter = new object[1];
            //Get the excel object
            outlook_app_type = Type.GetTypeFromProgID("Outlook.Application");
            //Create instance of excel
            oApp = Activator.CreateInstance(outlook_app_type);
            //Set the parameter which u want to set
            parameter[0] = "MAPI";
            //Set the Visible property
            oNameSpace = outlook_app_type.InvokeMember("GetNamespace",
            BindingFlags.InvokeMethod, null, oApp, parameter);

            var Logon_parameter = new object[4] { null, null, true, true };
            oNameSpace.GetType().InvokeMember("Logon",
            BindingFlags.InvokeMethod, null, oNameSpace, Logon_parameter);

            var GetDefaultFolder_parameter = new object[1] { 6 };
            oOutboxFolder =
            oNameSpace.GetType().InvokeMember("GetDefaultFolder",
            BindingFlags.InvokeMethod, null, oNameSpace,
            GetDefaultFolder_parameter);

            Console.WriteLine(messageNumber + " - Press enter to exit");
        }

        static void Main(string[] args)
        {
            //int messageNumber = 0;
            while (true)
            {
                messageNumber += 1;
                try
                {
                    OutlookEmailerLateBinding app = new OutlookEmailerLateBinding();
                    app.SendOutlookEmail("user1@domain.com; user2@domain.com", "Duplicate test message email " + messageNumber, "Test message. Testing. Only reply back if you see a duplicate.");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error Stack {0} ", e.Message);
                }

                //Console.ReadLine();

                Thread.Sleep(5000);
            }
        }

        public void SendOutlookEmail(string toValue, string
subjectValue, string bodyValue)
        {
            var CreateItem_parameter = new object[1] { 0 };
            object oMailItem =
oApp.GetType().InvokeMember("CreateItem", BindingFlags.InvokeMethod,
null, oApp, CreateItem_parameter);

            var mail_item_type = oMailItem.GetType();
            mail_item_type.InvokeMember("To",
                BindingFlags.SetProperty, null, oMailItem, new
object[] { toValue });
            mail_item_type.InvokeMember("Subject",
                BindingFlags.SetProperty, null, oMailItem, new
object[] { subjectValue });
            mail_item_type.InvokeMember("Body",
                BindingFlags.SetProperty, null, oMailItem, new
object[] { bodyValue });
            mail_item_type.InvokeMember("Send",
                BindingFlags.InvokeMethod, null, oMailItem, null);

        }

    }
}




On a side note, here is how to do it in PowerShell in a slightly different way by connecting directly to the Client Access Server (CAS).
.
if ( (Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.Exchange.Management.PowerShell.Admin"}) -eq $null )
  {
    add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin
  }


 $emailattachment = "xx" # full path to attachment file

 $body = "xx" # body of email

 $smtp = new-object Net.Mail.SmtpClient("xxx") # Dns name or IP of your cas server
 $smtpuser = new-object System.Net.networkCredential
 $smtpuser.domain = "xx" # your domain
 $smtpuser.username = "xx" # your username
 $smtpuser.password = "xx" # your password
 $smtp.Credentials = $smtpuser
 
 $SmtpClient = new-object system.net.mail.smtpClient
 $MailMessage = New-Object system.net.mail.mailmessage
 $SmtpClient.Host = "xxx" # Dns name or IP of cas server
 $mailmessage.from = "xxx" # your email address
 $mailmessage.To.add("xx@xx.xx") # email address to send to
 $mailmessage.Subject = "xxx" # subject
 $MailMessage.IsBodyHtml = $False
 $mailmessage.Body = $body 
 $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain') # you may have to change the file type or encrypt file with 7zip to get around gmail file attachment restrictions
   $mailmessage.Attachments.Add($attachment)
 $smtp.Send($MailMessage)