【问题标题】:Errors Using Python to Send Outlook Email使用 Python 发送 Outlook 电子邮件时出错
【发布时间】:2018-08-20 15:27:36
【问题描述】:

我正在尝试使用 python 在 Outlook 中发送电子邮件并遇到错误。我不确定问题的原因。它可能与服务器有关,但错误似乎表明它与脚本有关。电子邮件脚本是:

import win32com.client as win32
import psutil
import os
import subprocess

def send_notification():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = 'me@mycompany.com', 
    mail.Subject = 'Sent through Python'
    mail.body = 'This email alert is auto generated. Please do not respond.'
    mail.send

# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below

def open_outlook():
    try:
        subprocess.call(['C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe'])
        os.system("C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe");
    except:
        print("Outlook didn't open successfully")

# Checking if outlook is already opened. If not, open Outlook.exe and send email
for item in psutil.pids():
    p = psutil.Process(item)
    if p.name() == "OUTLOOK.EXE":
        flag = 1
        break
    else:
        flag = 0

if (flag == 1):
    send_notification()
else:
    open_outlook()
    send_notification()

我收到的错误消息是:

“文件“C:\Users***\Desktop\CORE\Query.py”,第 78 行,在 发送通知()

文件“C:\Users****\Desktop\CORE\Query.py”,第 53 行,在 send_notification 中 mail.To = '@.com',

文件“C:\Python27\lib\site-packages\win32com\client\dynamic.py”,第 565 行,在 setattr self.oleobj.Invoke(entry.dispid, 0, invoke_type, 0, value)"

pywintypes.com_error: (-2147352567, '发生异常。', (4096, u'Microsoft Outlook', u'对象不支持此方法。', None, 0, -2147352567), None)"

如果有人可以就我可以做些什么来使脚本正常工作提供一些建议,我将不胜感激。

谢谢!

【问题讨论】:

  • 不能确定这是问题的原因,所以我不会回答,但我相信MailItem.To 需要一个字符串列表而不是单个字符串。尝试将其设为mail.To = ['me@mycompany.com']。此外,您在该行后面还有一个逗号,可能不应该在那里。
  • 正如@GarrettGutierrez 所说,逗号可能是错误,只写mail.To = 'me@mycompany.com' 可以正常工作。去看看this question
  • 是的,是逗号。感谢您的帮助!
  • 发送是一个方法:mail.send()

标签: python outlook


【解决方案1】:

你可以试试这个吗?这行得通

import win32com.client as win32
import psutil
import os
import subprocess

def send_notification():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = 'forcepointtester1@outlook.com'
    mail.Subject = 'Sent through Python'
    mail.body = 'This email alert is auto generated. Please do not respond.'
    mail.Send()


# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below

def open_outlook():
    try:
        subprocess.call(['C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe'])
        os.system("C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe");
    except:
        print("Outlook didn't open successfully")

# Checking if outlook is already opened. If not, open Outlook.exe and send email
for item in psutil.pids():
    p = psutil.Process(item)
    if p.name() == "OUTLOOK.EXE":
        flag = 1
        break
    else:
        flag = 0

if (flag == 1):
    send_notification()
else:
    open_outlook()
    send_notification()

【讨论】:

  • 能否请您建议如何通过这种方法将邮件发送到多个电子邮件,对于单个收件人来说效果很好。谢谢
【解决方案2】:

嗨所以下面的代码对我有用,而且非常简单,你的代码似乎有点到处都是。

import win32com.client

inbox = win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI")
print(dir(inbox))
inbox = win32com.client.Dispatch("Outlook.Application")
print(dir(inbox))

mail = inbox.CreateItem(0x0)
mail.To = "testTo@test.com"
mail.CC = "testcc@test.com"
mail.Subject = "Send TEST"
mail.Body = "This is the body"
mail.Attachments.Add(u"path to attachment")
mail.Send()

请记住,在附件中要在 windows 系统上使用转义字符,即

c:\users\me  should be  c:\\users\\me

【讨论】:

    猜你喜欢
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2023-02-11
    相关资源
    最近更新 更多