The*_*CAL 111
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'To address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
attachment = "Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()
Run Code Online (Sandbox Code Playgroud)
将使用您的本地Outlook帐户发送.
请注意,如果您尝试执行上面未提及的操作,请查看COM文档属性/方法:https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/mailitem-object-outlook.在上面的代码中,mail
是一个MailItem对象.
Ste*_*end 28
通过Google查看,有很多示例,请参阅此处.
内联易于查看:
import win32com.client
def send_mail_via_com(text, subject, recipient, profilename="Outlook2003"):
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()
Run Code Online (Sandbox Code Playgroud)
Spe*_*bun 21
使用python附带的smtplib.请注意,这将要求您的电子邮件帐户允许smtp,默认情况下不一定启用.
SERVER = "smtp.example.com"
FROM = "yourEmail@example.com"
TO = ["listOfEmails"] # must be a list
SUBJECT = "Subject"
TEXT = "Your Text"
# Prepare actual message
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
import smtplib
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)
编辑:此示例使用RFC2606中描述的保留域
SERVER = "smtp.example.com"
FROM = "johnDoe@example.com"
TO = ["JaneDoe@example.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This is a test of emailing through smtp of example.com."
# Prepare actual message
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
import smtplib
server = smtplib.SMTP(SERVER)
server.login("MrDoe", "PASSWORD")
server.sendmail(FROM, TO, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)
为了实际使用gmail,Doe先生需要转到gmail中的选项选项卡并将其设置为允许smtp连接.
请注意添加登录行以对远程服务器进行身份验证.原始版本不包括这个,我的疏忽.
Office 365 的一个简单解决方案是
from O365 import Message
html_template = """
<html>
<head>
<title></title>
</head>
<body>
{}
</body>
</html>
"""
final_html_data = html_template.format(df.to_html(index=False))
o365_auth = ('sender_username@company_email.com','Password')
m = Message(auth=o365_auth)
m.setRecipients('receiver_username@company_email.com')
m.setSubject('Weekly report')
m.setBodyHTML(final)
m.sendMessage()
Run Code Online (Sandbox Code Playgroud)
这df
是一个转换为 html 表的数据框,它被注入到html_template
这是一个很老的问题,但还有一个解决方案。当前的 Outlook SMTP 服务器是(截至 2022 年):
smtp.office365.com
587
用于 TLS)也许最简单、最干净的解决方案是使用已经设置了这些的Red Mail :
pip install redmail
Run Code Online (Sandbox Code Playgroud)
然后:
from redmail import outlook
outlook.user_name = "example@hotmail.com"
outlook.password = "<MY PASSWORD>"
outlook.send(
receivers=["you@example.com"],
subject="An example",
text="Hi, this is an example."
)
Run Code Online (Sandbox Code Playgroud)
Red Mail 支持各种高级功能:
链接:
免责声明:我是作者
使用pywin32:
from win32com.client import Dispatch
session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\nUserName');
msg = session.Outbox.Messages.Add('Hello', 'This is a test')
msg.Recipients.Add('Corey', 'SMTP:corey@foo.com')
msg.Send()
session.Logoff()
Run Code Online (Sandbox Code Playgroud)
我想使用 SMTPLIB 发送电子邮件,它更容易并且不需要本地设置。在其他答案没有直接帮助之后,这就是我所做的。
在浏览器中打开 Outlook;转到右上角,单击“设置”的齿轮图标,从出现的下拉列表中选择“选项”。转到“帐户”,单击“流行和 Imap”,您将看到选项:“让设备和应用程序使用流行”,
选择是选项并保存更改。
这是后面的代码;必要时进行编辑。最重要的是启用POP和这里的服务器代码;
import smtplib
body = 'Subject: Subject Here .\nDear ContactName, \n\n' + 'Email\'s BODY text' + '\nYour :: Signature/Innitials'
try:
smtpObj = smtplib.SMTP('smtp-mail.outlook.com', 587)
except Exception as e:
print(e)
smtpObj = smtplib.SMTP_SSL('smtp-mail.outlook.com', 465)
#type(smtpObj)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('me@outlook.com', "password")
smtpObj.sendmail('sender@outlook.com', 'recipient@gmail.com', body) # Or recipient@outlook
smtpObj.quit()
pass
Run Code Online (Sandbox Code Playgroud)