You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows应用调用Outlook发件遇Interop兼容问题,求Outlook2021替代方案

兼容Outlook 2021的邮件发送替代方案

问题概述

在Windows应用中通过Outlook发送邮件时,触发Microsoft.Office.Interop.Outlook.ApplicationClass类型转换错误,具体报错为:

无法将类型为‘Microsoft.Office.Interop.Outlook.ApplicationClass’的COM对象强制转换为接口类型‘Microsoft.Office.Interop.Outlook._Application’

经确认旧版Microsoft.Office.Interop.Outlook.dll不兼容Outlook 2021,以下是可行的替代方案:


方案1:升级并正确使用Office Interop程序集

步骤

  1. 卸载项目中旧版Microsoft.Office.Interop.Outlook NuGet包,安装v15.0.4797.1003及以上版本(该版本适配Outlook 2016/2019/2021)
  2. 调整代码:改用_Application接口实例化Outlook对象,避免COM类型转换冲突

修正后的代码

using Outlook = Microsoft.Office.Interop.Outlook;
using System.IO;

public bool SendEmailThroughOUTLOOK()
{
    // 使用_Application接口而非ApplicationClass实例化
    Outlook._Application oApp = new Outlook.Application();            
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    Outlook.Recipients oRecips = oMsg.Recipients;
    Outlook.Recipient oRecip = null;

    try
    {
        if (Directory.GetFiles(WIPFolder).Length != 0)
        {
            string[] files = Directory.GetFiles(WIPFolder);                   
            string sDisplayName = "FeedbackForm";
            int iPosition = (int)oMsg.Body.Length + 1;
            int iAttachType = (int)Outlook.OlAttachmentType.olByValue;

            // 批量添加附件
            foreach (string sourceFile in files)
            {
                _ = oMsg.Attachments.Add(sourceFile, iAttachType, iPosition, sDisplayName);
            }                   

            // 初始化收件人并解析
            oRecip = oRecips.Add("目标收件人邮箱");
            oRecip.Resolve();              
            oMsg.Send();      

            return true;                 
        }
    }
    catch (Exception ex)
    {
        // 可添加错误日志记录逻辑
        return false;
    }
    finally
    {
        // 释放COM对象
        oRecip = null;
        oRecips = null;
        oMsg = null;
        oApp = null;
    }

    return false;
}

方案2:使用Microsoft Graph API(推荐)

无需依赖本地Outlook客户端,直接调用微软云服务发送邮件,兼容所有现代Outlook版本(包括2021),稳定性和扩展性更强。

核心步骤

  1. 在Azure AD注册应用,获取客户端ID、租户ID和客户端密钥,并申请Mail.Send权限
  2. 安装Microsoft.Graph和Azure.Identity NuGet包

示例代码

using Microsoft.Graph;
using Azure.Identity;
using System.IO;
using System.Collections.Generic;

public async Task<bool> SendEmailViaGraphAPI()
{
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var clientId = "你的Azure应用客户端ID";
    var tenantId = "你的租户ID";
    var clientSecret = "你的客户端密钥";

    var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    var graphClient = new GraphServiceClient(credential, scopes);

    try
    {
        var message = new Message
        {
            Subject = "邮件主题",
            Body = new ItemBody
            {
                ContentType = BodyType.Text,
                Content = "邮件正文内容"
            },
            ToRecipients = new List<Recipient>
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "目标收件人邮箱"
                    }
                }
            },
            Attachments = new List<Attachment>()
        };

        // 添加附件
        foreach (var filePath in Directory.GetFiles(WIPFolder))
        {
            var fileBytes = File.ReadAllBytes(filePath);
            message.Attachments.Add(new FileAttachment
            {
                Name = Path.GetFileName(filePath),
                ContentBytes = fileBytes
            });
        }

        await graphClient.Me.SendMail.PostAsync(new SendMailPostRequestBody
        {
            Message = message,
            SaveToSentItems = true
        });

        return true;
    }
    catch (Exception ex)
    {
        // 记录错误日志
        return false;
    }
}

方案3:直接使用SMTP协议发送

无需依赖Outlook客户端,通过SMTP直接连接邮件服务器发送,支持Outlook.com/Exchange等服务。

示例代码

using System.Net;
using System.Net.Mail;
using System.IO;

public bool SendEmailViaSMTP()
{
    try
    {
        using (var smtpClient = new SmtpClient("smtp.office365.com", 587))
        {
            smtpClient.EnableSsl = true;
            // 若开启两步验证,需使用应用专用密码
            smtpClient.Credentials = new NetworkCredential("你的Outlook邮箱账号", "密码/应用专用密码");

            var mailMessage = new MailMessage
            {
                From = new MailAddress("你的Outlook邮箱账号"),
                Subject = "邮件主题",
                Body = "邮件正文内容"
            };
            mailMessage.To.Add("目标收件人邮箱");

            // 添加附件
            foreach (var filePath in Directory.GetFiles(WIPFolder))
            {
                mailMessage.Attachments.Add(new Attachment(filePath));
            }

            smtpClient.Send(mailMessage);
            return true;
        }
    }
    catch (Exception ex)
    {
        // 记录错误日志
        return false;
    }
}

原代码说明

注:代码中已修正强制转换的语法错误

内容的提问来源于stack exchange,提问作者Nazia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:05:12