gpt4 book ai didi

python - 使用 Python 发送 Outlook 邮件的错误

转载 作者:行者123 更新时间:2023-12-05 05:17:26 25 4
gpt4 key购买 nike

我正在尝试使用 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',

setattr 中的文件“C:\Python27\lib\site-packages\win32com\client\dynamic.py”,第 565 行 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

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

谢谢!

最佳答案

你能试试这个吗?这行得通

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

关于python - 使用 Python 发送 Outlook 邮件的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49238127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com