1

We currently use Microsoft.Office.Interop.Outlook to Open Outlook with a preformatted MailItem. This is so the user can change any information before the email is sent.

Searched Exchange WebServices but did not find equivalent.

I cleaned the code below as much as possible. We would like to open a message in the current version of Outlook on our users Desktops. For now, we have Exchange on prem and Office 2010 we are moving to Office 2016 and O365 in the cloud.

    Public Function OpenOutlookSendWithAttachment(ByVal Subject As String, ByVal Body As String, ByVal FileName As String) As Boolean
        Dim bSuccess As Boolean = True
        Dim OutlookApplication As Microsoft.Office.Interop.Outlook.Application
        Dim OutlookMailItem As Microsoft.Office.Interop.Outlook.MailItem

        Try
            OutlookApplication = New Microsoft.Office.Interop.Outlook.Application
            Try
                If OutlookApplication.Session.Offline Then OutlookApplication.Session.Logon("", "", True, True)
            Catch ex As Exception
            End Try
            OutlookMailItem = OutlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
            OutlookMailItem.Subject = Subject
            OutlookMailItem.Body = Body & vbNewLine
            OutlookMailItem.Attachments.Add(FileName)
            OutlookMailItem.Display(True)
        Catch ex As Exception
            bSuccess = False
        Finally
            OutlookMailItem = Nothing
            OutlookApplication = Nothing
        End Try

        Return bSuccess
    End Function

Looking for any .Net method to perform the equivalent.

braX
  • 11,506
  • 5
  • 20
  • 33
thinkjohn
  • 33
  • 8
  • You do not explain why you need a .Net solution. I would convert this VB.Net code to an Outlook VBA macro. VBA’s `On Error` is the equivalent of VB.Net’s `Try` block although it is not as convenient. The code to open Outlook would not be needed because the macro would be within Outlook. Everything else just uses the InterOp to call standard Outlook VBA statements. – Tony Dallimore Aug 16 '19 at 20:12

2 Answers2

1

You can use either a mailto link (which does not support attachments) or create an MSG (binary) or EML (MIME text) file: Outlook will be happy to open and show it.

In the latter case (.EML file), don't forget to add "X-Unsent: 1" MIME header to force Outlook to treat the message as unsent and show the Send button.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Creating the .EML file works great. It is not easy as you have to use reflection and create an Extension to Save a System.Net.Mail.MailMessage. – thinkjohn Sep 09 '19 at 18:19
  • EML is a text file (you can open it in Notepad) - you can create a template once and just insert your custom data in the relevant places at run-time. – Dmitry Streblechenko Sep 09 '19 at 20:04
0

There is no other way to open the installed Outlook version with a preformatted email except the mailto protocol. Technology remains the same through all Outlook versions. See C# app automates Outlook (CSAutomateOutlook) for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45