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

Mac下用Python自动化Outlook并实现邮件发送的问题

正确的Mac Outlook邮件发送方法

你代码里的self.msg.Send()无法生效,核心原因是在py-appscript库中,Mac Outlook的outgoing_message对象发送方法是小写的send(),AppleScript的方法名在py-appscript中会转换为Python风格的小写调用,而非你写的大写Send()。

修改后的发送方法

直接把send_email方法改成如下即可:

def send_email(self):
    self.msg.send()

补充完整的收件人处理(避免邮件无收件人导致发送失败)

另外要确保你在Message类的__init__方法里正确添加了收件人,否则即使调用发送方法也会失败,完整的收件人添加代码如下:

class Message(object):
    def __init__(self, parent=None, subject='', body='', to_recip=[], cc_recip=[], show_=True):
        if parent is None: parent = Outlook()
        client = parent.client

        self.msg = client.make(
            new=k.outgoing_message,
            with_properties={k.subject: subject, k.content: body})

        # 逐个添加收件人
        for recipient in to_recip:
            recip = client.make(
                new=k.recipient,
                with_properties={k.email_address: {k.address: recipient}}
            )
            self.msg.recipients.push(recip)
        
        # 可选:添加抄送收件人
        for cc in cc_recip:
            cc_recip_obj = client.make(
                new=k.recipient,
                with_properties={k.email_address: {k.address: cc}}
            )
            self.msg.cc_recipients.push(cc_recip_obj)
        
        # 如果需要先打开邮件窗口再发送(可选)
        if show_:
            self.msg.open()
            self.msg.activate()

其他注意事项

  • 确保已安装依赖库:执行pip install py-appscript
  • 首次运行时,系统会弹出权限请求,需要允许Python控制Microsoft Outlook
  • 若邮件带附件,需确保附件已正确添加到self.msg.attachments中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 16:25:19