I am building an ASP webforms application in C# and I am trying to launch Outlook to send an email from the client computer.
I am using the following example, which I found online.
public void sendEmail(object sender, EventArgs e)
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Get the MAPI namespace.
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using the default profile or existing session (no dialog box).
oNS.Logon(Missing.Value, Missing.Value, false, true);
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
oMsg.HTMLBody = "Test";
//Subject line
oMsg.Subject = "Test Subject";
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip;
oRecip = (Outlook.Recipient)oRecips.Add("[email protected]");
oRecip.Resolve();
// Send.
oMsg.Send();
//Log off.
oNS.Logoff();
}
I am having issues at this line of code here:
oNS.Logon(Missing.Value, Missing.Value, false, true);
Even though I have an Outlook profile configured, when it runs this line of code, my app launches Outlook and displays the "Welcome to Outlook 2016" dialogue.
The expected behaviour is for it to launch Outlook, using the existing profile.
If Outlook is already open, I get an error stating:
System.Runtime.InteropServices.COMException: 'Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).'
The expected behaviour is if Outlook is already open, is for my app to use the existing Outlook process.
Any help greatly appreciated.