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

Windows 11下Python调用Outlook New发送邮件失败求解决

解决Windows 11下Outlook New无法通过win32com发送邮件的问题

问题原因

你使用的Outlook New是基于WebView的轻量版本,未提供传统Outlook桌面端的COM接口,因此win32com.client.Dispatch("Outlook.Application")会因找不到对应COM类触发报错。


解决办法

1. 安装原生Outlook桌面端

这是最直接的方案,可直接复用原有代码:

  • 打开微软商店搜索「Outlook」,选择带有「桌面版」标识的版本安装;
  • 或通过Office 365/2024套件安装完整的Outlook客户端。
    安装完成后,原win32com代码即可正常运行。

2. 改用SMTP协议直接发送邮件

不依赖任何桌面客户端,用Python内置库实现,支持绝大多数邮箱服务商:

示例代码(以Outlook.com邮箱为例):

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 配置信息
sender_email = "your-account@outlook.com"
sender_password = "your-app-password"  # Outlook需使用应用专用密码,而非登录密码
receiver_email = "valid@email.here"
smtp_server = "smtp.office365.com"
smtp_port = 587

# 构造邮件
message = MIMEText("Email body, Python SMTP.", "plain", "utf-8")
message["Subject"] = Header("Email Subject", "utf-8")
message["From"] = sender_email
message["To"] = receiver_email

# 发送邮件
try:
    server = smtplib.SMTP(smtp_server, smtp_port)
    server.starttls()  # 启用TLS加密
    server.login(sender_email, sender_password)
    server.sendmail(sender_email, receiver_email, message.as_string())
    print("邮件发送成功")
except Exception as e:
    print(f"发送失败: {e}")
finally:
    server.quit()

注意事项:

  • 不同邮箱的SMTP服务器/端口不同(如Gmail为smtp.gmail.com,端口587);
  • 多数邮箱需开启「POP3/SMTP服务」或生成「应用专用密码」,操作细节参考对应邮箱帮助文档。

3. 使用Microsoft Graph API发送邮件

若需调用Outlook更多功能(如读取邮件、日历)或企业环境使用,可采用Microsoft Graph API:

示例代码(需先注册Azure应用获取权限):

import requests

# 替换为你的配置信息
client_id = "your-client-id"
client_secret = "your-client-secret"
tenant_id = "your-tenant-id"
sender_email = "your-account@outlook.com"
receiver_email = "valid@email.here"

# 获取访问令牌
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
token_data = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": "https://graph.microsoft.com/.default"
}
token_response = requests.post(token_url, data=token_data)
access_token = token_response.json()["access_token"]

# 构造邮件内容
email_data = {
    "message": {
        "subject": "Email Subject",
        "body": {
            "contentType": "Text",
            "content": "Email body, Python Microsoft Graph API."
        },
        "toRecipients": [{"emailAddress": {"address": receiver_email}}]
    },
    "saveToSentItems": "true"
}

# 发送邮件
send_url = f"https://graph.microsoft.com/v1.0/users/{sender_email}/sendMail"
headers = {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"}
response = requests.post(send_url, json=email_data, headers=headers)

if response.status_code == 202:
    print("邮件发送成功")
else:
    print(f"发送失败: {response.json()}")

说明:

  • 需在Azure Active Directory中注册应用,并授予Mail.Send应用权限;
  • 此方案适合企业级开发,个人使用推荐SMTP方案更简便。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 16:13:23