此代码可以正常工作并向我发送电子邮件:
import smtplib
#SERVER = "localhost"
FROM = 'monty@python.com'
TO = ["jon@mycompany.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This message was sent with Python's smtplib."
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将其包装在这样的函数中:
def sendMail(FROM,TO,SUBJECT,TEXT,SERVER):
import smtplib
"""this is some test documentation in the function"""
message = """\
From: %s
To: …Run Code Online (Sandbox Code Playgroud) 这个问题是因为行:mail = Outlook.CreateItem(0)吗?我想在Python中的for循环中发送n封电子邮件。
for aaa in jira.search_issues(JQL,startAt=0, maxResults=50):
print(aaa)
try:
tworca = (jira.issue(aaa).fields.creator.name)
przypisany =(jira.issue(aaa).fields.assignee.name)
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = tworca + ';' + przypisany
mail.Subject = 'blablabla'
mail.Body = 'Message body'
mail.send()
print ("OK!")
except Exception as e:
print("ERROR: " + str(e))
print ("done!")
Run Code Online (Sandbox Code Playgroud)
追溯:
Traceback (most recent call last):
File "C:\xxx\xxx\xxx\xxx.py", line 12, in <module>
mail.send()
TypeError: 'bool' object is not callable
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python启动一些Outlook 2007自动化.我从Steve Townsend那里得到了这个伟大的剧本(下面):通过Python发送Outlook电子邮件?
但是我很难开始这个.
import win32com.client
def send_mail_via_com(text, subject, recipient, profilename="Outlook2007"):
s = win32com.client.Dispatch("Mapi.Session")
o = win32com.client.Dispatch("Outlook.Application")
s.Logon(profilename)
Msg = o.CreateItem(0)
Msg.To = recipient
Msg.CC = "moreaddresses here"
Msg.BCC = "address"
Msg.Subject = subject
Msg.Body = text
#attachment1 = "Path to attachment no. 1"
#attachment2 = "Path to attachment no. 2"
#Msg.Attachments.Add(attachment1)
#Msg.Attachments.Add(attachment2)
Msg.Send()
send_mail_via_com("test text","test subject", "removed@security.obv","Outlook2007")
Run Code Online (Sandbox Code Playgroud)
但我得到以下错误:
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 83, in _
GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', …Run Code Online (Sandbox Code Playgroud)