使用Python发送Outlook电子邮件时出现错误

3

我正在尝试使用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()

我收到的错误信息如下:

"File "C:\Users***\Desktop\CORE\Query.py", line 78, in send_notification()

File "C:\Users****\Desktop\CORE\Query.py", line 53, in send_notification mail.To = '@.com',

File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 565, in setattr self.oleobj.Invoke(entry.dispid, 0, invoke_type, 0, value)"

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'The object does not support this method.', None, 0, -2147352567), None)"

enter image description here

如果有人能提供一些关于如何让脚本正常运行的建议,我将不胜感激。

谢谢!


1
不能确定这是否是问题的原因,所以我不会回答,但我相信 MailItem.To 接受一个字符串列表而不是单个字符串。尝试将其更改为 mail.To = ['me@mycompany.com']。此外,您在该行之后有一个逗号,这可能不是您想要的。 - Garrett Gutierrez
正如@GarrettGutierrez所说,逗号可能是错误的原因,只写mail.To = 'me@mycompany.com'通常可以正常工作。请查看此问题 - Ben.T
是的,是逗号的问题。感谢您的帮助! - JJMeadows3
1
发送是一种方法:mail.send() - Dmitry Streblechenko
2个回答

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()

请问您能否建议如何通过这种方法向多个电子邮件地址发送邮件,对于单个收件人它运行良好。谢谢。 - Chandra Shekhar

1

嗨,以下内容对我有效且非常简单,你的代码似乎有点混乱。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接