Outlook Programmatic Access

E

Ed

I'm using Outlook 2007 and have written code in Visual Basic 2008 to send
emails through Outlook. When I run my code, a message pops up saying that
another program is trying to use Outlook to send an email, and I can Allow or
Deny. I see in Outlook under Tools->Trust Center->Programmatic Access that I
can turn off any checking for this. However, how can I tell Outlook to trust
my program (and not ask for Allow or Deny) but still check for other programs
(or viruses) trying to send emails?
 
T

Tim Li - MSFT

Hello Ed,

Outlook does not native support this feature of choosing which application
should be trusted. For the concept of Outlook Security please refer to this
link:
http://www.outlookcode.com/article.aspx?ID=52
Personally, I agree with above article, thus, we have several choices to
avoid the prompt, such as use another API instead of Outlook Object Model,
due to your scenario, there are two options better than the other choices.
The first is use a third party add-in to set which application is trusted:
http://www.mapilab.com/outlook/security/

However this is a third party solution, we not tested any software or
information found on this site; therefore, Microsoft cannot make any
representations regarding the quality, safety, or suitability of any
software or information found there. There are inherent dangers in the use
of any software found on the Internet, and Microsoft cautions you to make
sure that you completely understand the risk before retrieving any software
from the Internet.

The second solution is we could write a VSTO add-in for Outlook, and expose
a method to send mail from that add-in, therefore, the mail actually is
sent by that in-process COM Add-in of Outlook, due to the security model of
Outlook, it will trust all COM Add-In by default, and we call the send mail
code of this add-in from other application will not trigger the security
prompt.

The concept of this method is list as below:
http://msdn.microsoft.com/en-us/library/bb226709.aspx
http://msdn.microsoft.com/en-us/library/bb608621.aspx

Here's a sample of this solution which I have tested in my side:

public partial class ThisAddIn
{
private AddInUtilities utilities;

protected override object RequestComAddInAutomationService()
{
if (utilities == null)
utilities = new AddInUtilities();

return utilities;
}

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}


[ComVisible(true)]
public interface IAddInUtilities
{
void ImportData();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{

public void ImportData()
{

Outlook.MailItem mi =
Globals.ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.Ol
ItemType.olMailItem) as Outlook.MailItem;
mi.To = "(e-mail address removed)";
mi.Subject = DateTime.Now.ToString();
mi.Send();

}
}
before we build this add-in we need to check the option in Project
Properties -> Application tab ->Assembly Information->COM Visible,
and Project Properties -> Build tab ->Register for COM interop.

In the other side I take a WinForm application for example, please note
that we have to cast the COMAddin.Object back to IAddInUtilities.
using OutlookAddInCallFromWinForm;

public Form1()
{
InitializeComponent();
Outlook.Application app =
Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;

object index = 14;
OutlookAddInCallFromWinForm.IAddInUtilities iaddin =
app.COMAddIns.Item(ref index).Object as
OutlookAddInCallFromWinForm.IAddInUtilities;
iaddin.ImportData();
}

This code is in C# language, if you have any problem during translate it
into Visual Basic please feel free to follow up.


Best regards,
Tim Li
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
L

Lawrence

Hi Tim,

I am uisng Visual Studio 2008 and am getting the following error..

RequestComAddInAutomationService() no suitable method for

I have the following references
Microsoft.Office.Core
Microsoft.Office.Interop.Outlook

How to get I

Here is the full code

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;

using Outlook = Microsoft.Office.Interop.Outlook;


namespace OutlookAddInCallFromWinForm
{
public partial class ThisAddIn
{
private AddInUtilities utilities;

protected override object RequestComAddInAutomationService()
{
if (utilities == null)
utilities = new AddInUtilities();

return utilities;
}

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAddInUtilities
{
void ImportData();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{
public void ImportData()
{

Outlook.MailItem mi =
ThisAddIn.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
as Outlook.MailItem;
mi.To = "(e-mail address removed);
mi.Subject = DateTime.Now.ToString();
mi.Send();

}
}
}

Tim Li - MSFT said:
Hello Ed,

Outlook does not native support this feature of choosing which application
should be trusted. For the concept of Outlook Security please refer to this
link:
http://www.outlookcode.com/article.aspx?ID=52
Personally, I agree with above article, thus, we have several choices to
avoid the prompt, such as use another API instead of Outlook Object Model,
due to your scenario, there are two options better than the other choices.
The first is use a third party add-in to set which application is trusted:
http://www.mapilab.com/outlook/security/

However this is a third party solution, we not tested any software or
information found on this site; therefore, Microsoft cannot make any
representations regarding the quality, safety, or suitability of any
software or information found there. There are inherent dangers in the use
of any software found on the Internet, and Microsoft cautions you to make
sure that you completely understand the risk before retrieving any software
from the Internet.

The second solution is we could write a VSTO add-in for Outlook, and expose
a method to send mail from that add-in, therefore, the mail actually is
sent by that in-process COM Add-in of Outlook, due to the security model of
Outlook, it will trust all COM Add-In by default, and we call the send mail
code of this add-in from other application will not trigger the security
prompt.

The concept of this method is list as below:
http://msdn.microsoft.com/en-us/library/bb226709.aspx
http://msdn.microsoft.com/en-us/library/bb608621.aspx

Here's a sample of this solution which I have tested in my side:

public partial class ThisAddIn
{
private AddInUtilities utilities;

protected override object RequestComAddInAutomationService()
{
if (utilities == null)
utilities = new AddInUtilities();

return utilities;
}

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}


[ComVisible(true)]
public interface IAddInUtilities
{
void ImportData();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{

public void ImportData()
{

Outlook.MailItem mi =
Globals.ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.Ol
ItemType.olMailItem) as Outlook.MailItem;
mi.To = "(e-mail address removed)";
mi.Subject = DateTime.Now.ToString();
mi.Send();

}
}
before we build this add-in we need to check the option in Project
Properties -> Application tab ->Assembly Information->COM Visible,
and Project Properties -> Build tab ->Register for COM interop.

In the other side I take a WinForm application for example, please note
that we have to cast the COMAddin.Object back to IAddInUtilities.
using OutlookAddInCallFromWinForm;

public Form1()
{
InitializeComponent();
Outlook.Application app =
Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;

object index = 14;
OutlookAddInCallFromWinForm.IAddInUtilities iaddin =
app.COMAddIns.Item(ref index).Object as
OutlookAddInCallFromWinForm.IAddInUtilities;
iaddin.ImportData();
}

This code is in C# language, if you have any problem during translate it
into Visual Basic please feel free to follow up.


Best regards,
Tim Li
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top