简体   繁体   中英

Interacting with Outlook from an ASP.Net Core Blazor Web-site

I am converting an old Silverlight application into Asp.Net Core, using Blazor and Razor Pages.

The old application is opening Outlook, and creating an email ready to send with a Subject, From etc, plus a series of attachments. It is creating an object to do this using the following code:

Dynamic outlook = AutomationFactory.GetObject(“Outlook.Application”).

In my web-site, I do something similar:

This works when running my web-site locally in Visual Studio, however I cannot deploy it to Azure:

C:\Program Files\dotnet\sdk\6.0.201\Microsoft.Common.CurrentVersion.targets(2926,5): Error : MSB4803: The task "ResolveComReference" is not supported on the .NET Core version of MSBuild. Please use the .NET Framework version of MSBuild. See https://aka.ms/msbuild/MSB4803 for further details.

I'm assuming that I won't be able to use this approach, however I can't see any alternative. Any suggestions as to how I can do this in an ASP.Net Core Blazor web-site would be greatly appreciated.

You cannot use Outlook, or any other Office app, in a service (such as IIS). Even if you do manage to make it run, it won't help you - your code is running on the server, and the message you populate and display will be on the server, where nobody would ever see it.

You can either use a mailto: link (it supports address, subject, and body) or (if you need HTML and/or attachments), dynamically create an EML (MIME) file - Outlook on the client side will be happy to open it. To make sure the message is editable, add X-Unsent: 1 MIME header.

In my web-site, I do something similar: Use a COM reference to the Microsoft Outlook 16.0 object library.

The Considerations for server-side Automation of Office states the following:

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.

From a JS code you can automate Outlook from Inte.net Explorer (only on Windows). Other browsers don't know anything about the COM technology. So, I'd suggest considering other approaches for getting the work done. The easiest way, like Dmitry suggested, is to use the mailto protocol . It opens a client's e-mail system and begins a new email message. The following example shows a link that will prepare an e-mail message:

<a href="mailto:user@example.com?
    subject=MessageTitle&amp;
    body=Message Content">
    Contact Us</a>

You may consider using EWS or Graph API if you need to send email silently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM