I am new to Visual Studio 2017 and c#.
My goal is to open a new Outlook window by clicking a button in a small program I wrote for learning reasons.
The problem is that, as far as I know, the Office API look here does not support Office 2016, or better said, any Framework over 2.0
I only found this slightly helpful comment by a user on this side, but they also suggest the Office API which doesn't work anymore.
I am very thankful for every helpful comment!
It doesn't matter which Office interop files are used (to which Office version they belong) - you can still automate Office applications from .Net applications. So, just add a COM reference (for Microsoft Office Outlook) to your application and use the following code:
using System;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace FileOrganizer
{
class Program
{
private void CreateMailItem()
{
Outlook.Application app = new Outlook.Application();
Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
mailItem.To = "someone#example.com";
mailItem.Body = "This is the message.";
mailItem.Display(false);
}
}
}
Related
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("yyyyy#mydomain.co.uk");
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.
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
As a possible workaround, you may consider a low-level API on which Outlook is based on - Extended MAPI. Or just any wrapper around that API such as Redemption.
If you deal only with Exchange profiles you may consider using Exchange Web Services (EWS), see Start using web services in Exchange.
I am attempting to utilize Office interop with C#, but I'm having some difficulties. Executing a test like the one I included below seems to work insofar as it launches Outlook and seems to connect with it. The issue is that if I then try to open the Outlook window (it starts hidden in the tray) I get an error message from Outlook saying The application was unable to start correctly (0xc0000142). I do not get this error if Outlook was already running before I started my application. Am I doing something incorrectly or is something broken?
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookInteropTest1
{
class Program
{
static void Main(string[] args)
{
var app = new Outlook.Application();
Console.ReadKey();
}
}
}
Visual Studio Community 2017 Version 15.2
Office 360 - Outlook Version 1804 Build 9226.2156
Windows 10 Build 17115.1
EDIT: Tested this on Windows 7 and could not reproduce crash. I know that I had this working in Windows 10 at some point. I reinstalled my OS and it still crashes. I'm chocking this up to the typical Microsoft user experience unless anyone has any ideas on how to fix it.
Outlook is a singleton, so creating a new object will return the existing object if Outlook is already running.
In your case you also need to provide namespace to it
olApp = new Outlook.Application();
Outlook.Namespace ns = olApp.GetNamespace("MAPI");
ns.Logon();
I know this is old, but I was having the same issue and perhaps it will help someone in the future:
As IAmRajshah mentioned, only one istance of outlook can run, so, if Outlook is open your code olApp = new Outlook.Application();will crash, you need to "connect" to the active instance of outlook with somenthing like this Oulook.Application olApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; The link below has a good example of this:
Get and sign in to an instance of Outlook
I am trying to open an email in Outlook from within a .NET application. When I run everything on my local machine it works just fine. When I deploy out to an IIS8 server I get an error on loading of the page. Does Outlook need to be installed on the server as well as the local client or does it just need to be on the client? When I comment out the below code everything loads just fine.
using Microsoft.Office.Interop.Outlook;
using Outlook = Microsoft.Office.Interop.Outlook;
protected void passdownBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
{
SqlCommand comm = new SqlCommand("EXEC SvcGridEmail", conn);
conn.Open();
comm.ExecuteNonQuery();
string body = (string)comm.ExecuteScalar();
conn.Close();
string address = "bogus#email.com";
string time = String.Format("{0:MM/dd/yy HH:mm}", System.DateTime.Now);
string subject = "Service Jobs Passdown # " + time;
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = address;
oMailItem.HTMLBody = body;
oMailItem.Subject = subject;
oMailItem.Display(true);
}
}
Yes it absolutely has to be installed. Outlook != Exchange.
You're using Outlook to automate "Outlook the application" - How can you do that if it isn't installed?
Automating Exchange is another story.
Are there any reasons other than the dependency on Outlook being installed
nonsense
1.1. when you run it on your box - it starts Outlook using your account with your permissions, when running on server under ASP.NET account - it may not open any account even Outlook will be installed there
1.2. how do you want to see a server application (Outlook) within an ASP.NET website, which works within a browser window?
1.3 sending an email message does not require Outlook. See How Can i Send Mail Through Exchange Server by using SMTP If message needs to be modified before being sent - create a webform.
licensing (I bet your organization has no server licence for MS Office)
As the others have told you Outlook must be installed if you want to use Outlook.Application.
However, from your comments I read that you want to open an Outlook instance on the client, not on your server. This is not possible using Outlook.Application. As I see it you have two possibilities:
Follow the suggestion of #Alex K.
Create a mailto link for your customer. If your customer clicks it it will open the default mailing program (does not need to be Outlook) on your customer's machine with the content you have defined.
I am creating a button in asp.net c# that when clicked will open up Outlook window.
I am referencing to the Microsoft.Office.Interop.Outlook dll, and using this in using statement:
using Outlook = Microsoft.Office.Interop.Outlook;
This the code.
private void CreateMailItem()
{
try
{
var outlookApp = new Outlook.Application();
var mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
//var mailItem = (Outlook.MailItem)
// Application.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
mailItem.To = "someone#example.com";
mailItem.Body = "This is the message.";
mailItem.Importance = Outlook.OlImportance.olImportanceLow;
mailItem.Display(false);
}
catch (Exception)
{
throw;
}
}
I get error on the very first line, var outlookApp = new Outlook.Application();
The exception says:
{"Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))."}
The exception you've posted is thrown when a referenced dll or depencies of this dll are not correctly installed.
In this case, it seems outlook or office is not with the correct version which you referenced, on your test machine?
Can't post this as comment.
I would like to know why you prefer the use of Outlook Interop?
I am using the mailto:// protocol if I wanted my program to send email on the user's current email client, though I use this on WinForms.
like http://www.rapidtables.com/web/html/mailto.htm
Outlook, just like any Office app, cannot be used from a service (such as IIS). Even if you did make it work, the new message window will be displayed on the server where the user will not see it anyway.
You can try to run a client-side JavaScritp code, but then you'd be limited IE only, Outlook would need to be locally installed, and your site must be trusted to be able to create COM objects in a script.
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
Consider using System.Net.Mail namespace for creating and sending emails in ASP.NET.
I am trying to send email from .net windows application using Microsoft.Office.Interop.Outlook. The mail is in html format and has an image embedded to it. Getting the following error when trying to attach the image,
"Cannot create file: XXX.jpg. Right-click the folder you want to create the file in, and then click Properties on the shortcut menu to check your permissions for the folder."
I dont have any issues with folder access. I am running the app on 64 bit computer with 32 bit outlook on it. When i dont embed image, i dont get any errors and it works fine.
string body = string.Empty;
using(StreamReader reader = new StreamReader(Environment.CurrentDirectory + #"/Mail Templates/XXX.txt"))
{
body = reader.ReadToEnd();
}
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem mailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.To = "XXX";
string filename = Environment.CurrentDirectory + #"/Mail Templates/XXX.jpg";
mailItem.Attachments.Add(filename, (int)Outlook.OlAttachmentType.olEmbeddeditem, 1);
mailItem.HTMLbody = "true"
mailItem.Display(true);
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.
You can read more about that in the Considerations for server-side Automation of Office article.
You may consider using the EWS (Exchange Web Services) or BCL (Base Class Libraries from the .Net framework). See EWS Managed API, EWS, and web services in Exchange for more information.