如何使用Python+win32com Outlook发送发件人为no-reply@mycompany.com的邮件
Outlook设置代发邮箱解决方案
前置要求
- 你的主账号
myname@mycompany.com必须已获得企业IT管理员授予的no-reply@mycompany.com代发送权限,无权限时代发配置完全不生效 - 确认
no-reply@mycompany.com是企业域内有效、可正常接收/发送邮件的地址 - 可先手动打开Outlook客户端,新建邮件后手动在发件人栏选择/输入
no-reply@mycompany.com发送测试,确认手动可发送成功后再调试代码
方案1:修复原代发代码
原代码存在语法错误,且部分Outlook版本需要同时配置两个代发属性才能生效,修正后代码如下:
import win32com.client as client outlook = client.Dispatch("Outlook.Application") # 正确初始化邮件对象 message = outlook.CreateItem(0) # 配置代发属性 message.SendOnBehalfOfName = "no-reply@mycompany.com" message.SentOnBehalfOfEmailAddress = "no-reply@mycompany.com" # 其他邮件配置 message.To = "tocustomer@blabla.com" message.Subject = "test" message.HTMLBody = "test" message.Send()
方案2:直接指定发件账号
如果no-reply@mycompany.com已经在你本地Outlook客户端的账号列表中完成登录配置,可以直接指定该账号作为发件人,不需要代发权限:
import win32com.client as client outlook = client.Dispatch("Outlook.Application") message = outlook.CreateItem(0) # 遍历本地Outlook账号,匹配目标发件地址 for account in outlook.Session.Accounts: if account.SmtpAddress.lower() == "no-reply@mycompany.com": # 给SendUsingAccount属性赋值,固定调用写法适配所有win32com版本 message._oleobj_.Invoke(*(64209, 0, 8, 0, account)) break message.To = "tocustomer@blabla.com" message.Subject = "test" message.HTMLBody = "test" message.Send()
内容的提问来源于stack exchange,提问作者Ricardo Jacobsen
相关产品推荐
相关产品推荐

