Home > Mobile >  Outlook Interop - 'Class' does not contain a definition for 'Property'
Outlook Interop - 'Class' does not contain a definition for 'Property'

Time:08-03

Ultimate Goal/Frame Challenge:

I'm trying to append the user's name and e-mail address to a template e-mail before sending it.

Approach

I'd like to use the DisplayName and SmtpAddress properties of the first Account in the Outlook.Application.Session.Accounts collection using the below code. The e-mail template does appear, but the error occurs when I try to append the text:

var outlook = new Microsoft.Office.Interop.Outlook.Application();

MailItem mailItem = outlook.CreateItemFromTemplate(TemplateEmail.FullName) as MailItem;
mailItem.Display();

mailItem.HTMLBody  = (
    "For questions regarding this submission, please contact "   
    outlook.Session.Accounts[0].DisplayName   " at "   outlook.Session.Accounts[0].SmtpAddress
);

mailItem.Send();

Runtime error - not a compile error, Intellisense does like the properties

error CS1061: 'Application' does not contain a definition for 'Session'

What I've tried

  1. Close all open files in my solution
  2. Get the latest Outlook.Interop via Nuget, Microsoft signed
  3. Remove the older Outlook.Interop reference
  4. Save and close Visual Studio
  5. Delete my bin and obj folders and my solution's .suo file
  6. Restart my computer
  7. Re-open the project in Visual Studio
  8. Build --> Clean solution; Build --> Rebuild solution;

Outlook.Application properties at the time of the error

{Microsoft.Office.Interop.Outlook.ApplicationClass}
    AnswerWizard: '((Microsoft.Office.Interop.Outlook.ApplicationClass)outlook).AnswerWizard' threw an exception of type 'System.Runtime.InteropServices.COMException'
    Application: {Microsoft.Office.Interop.Outlook.ApplicationClass}
    Assistance: {System.__ComObject}
    Assistant: {System.__ComObject}
    COMAddIns: {System.__ComObject}
    Class: olApplication
    DefaultProfileName: "Outlook"
    Explorers: {Microsoft.Office.Interop.Outlook.ExplorersClass}
    FeatureInstall: msoFeatureInstallOnDemand
    Inspectors: {Microsoft.Office.Interop.Outlook.InspectorsClass}
    IsTrusted: false
    LanguageSettings: {System.__ComObject}
    Name: "Outlook"
    Parent: null
    PickerDialog: {System.__ComObject}
    Reminders: {Microsoft.Office.Interop.Outlook.RemindersClass}
    Session: {Microsoft.Office.Interop.Outlook.NameSpaceClass}
    TimeZones: {Microsoft.Office.Interop.Outlook.TimeZonesClass}
    Version: "16.0.0.14326"

I'm using 64-bit Outlook 365 on 64-bit Windows 10 for a 64 bit C# application.


Follow up question: C# class instance displays property values but does not allow direct referral

CodePudding user response:

First of all, you need to use a well-formed HTML markup when setting the HTMLBody property, for example, concatenating string doesn't work in the following way:

mailItem.HTMLBody  = (
    "For questions regarding this submission, please contact "   
    outlook.Session.Accounts[0].DisplayName   " at "   outlook.Session.Accounts[0].SmtpAddress
);

Instead, you need to find the opening <body> and closing </body> tags and paste the string between them. In that case you will get a well-formed HTML document which represents the message body.

To avoid errors with member definitions you need to declare the Outlook Application instance in the following way:

Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();

Also, try to add a COM reference manually in VS instead of using any NuGet packages:

Get the latest Outlook.Interop via Nuget, Microsoft signed

If you have multiple profile configured you need to use the Logon method to log into one of the profiles.

Then use the GetNamespace("MAPI") method instead of calling the Session property.

Be aware, the Send method may trigger security prompts or issues when Outlook is automated from external applications:

mailItem.Send();

You can read more about that in the Outlook Object Model Security Warnings article.

CodePudding user response:

All Outlook collections are 1 based, not 0. Use outlook.Session.Accounts[1].DisplayName.

Also, do not concatenate strings when building an HTML body. You need to insert your string inside the existing HTML body.

Thirdly, try to break that line and explicitly define variables:

Outlook.Application app = new Outlook.Application(); 
Outlook.Namespace ns = app.Session;
  • Related