通过Python发送Outlook电子邮件? [英] Send Outlook Email Via Python?

查看:1112
本文介绍了通过Python发送Outlook电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Outlook 2003.

使用Python发送电子邮件(通过Outlook 2003)的最佳方法是什么?

What is the best way to send email (through Outlook 2003) using Python?

推荐答案

有关使用Outlook的解决方案,请参见下面的TheoretiCAL答案.

For a solution that uses outlook see TheoretiCAL's answer below.

否则,请使用python随附的smtplib.请注意,这将要求您的电子邮件帐户允许smtp,默认情况下不一定启用此功能.

Otherwise, use the smtplib that comes with python. Note that this will require your email account allows smtp, which is not necessarily enabled by default.

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

编辑:该示例使用的保留域如 RFC2606 中所述

this example uses reserved domains like described in 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()

要使其与gmail一起实际使用,Doe先生需要进入gmail中的选项"标签并将其设置为允许smtp连接.

请注意,添加了用于对远程服务器进行身份验证的登录行.原始版本不包含此内容,这是我的疏忽.

Note the addition of the login line to authenticate to the remote server. The original version does not include this, an oversight on my part.

这篇关于通过Python发送Outlook电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆