无法从python发送电子邮件
我正在使用以下代码从localhost中的python程序发送电子邮件无法从python发送电子邮件,python,email,localhost,Python,Email,Localhost,我正在使用以下代码从localhost中的python程序发送电子邮件 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText me = "tonyr1291@gmail.com" you = "testaccount@gmail.com" msg = MIMEMultipart('alternative') msg['Subject'] = "Li
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "tonyr1291@gmail.com"
you = "testaccount@gmail.com"
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('localhost',5000)
s.sendmail(me, you, msg.as_string())
s.quit()
导入smtplib
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
我=”tonyr1291@gmail.com"
你=”testaccount@gmail.com"
msg=MIMEMultipart('alternative')
msg['Subject']=“链接”
msg['From']=我
msg['To']=您
text=“Hi!\n您怎么样?\n您想要的链接是:\nhttp://www.python.org"
html=”“”\
嗨!
你好吗?
这是你想要的。
"""
part1=MIMEText(文本“纯”)
part2=MIMEText(html,'html')
附加信息(第1部分)
附加信息(第2部分)
s=smtplib.SMTP('localhost',5000)
s、 sendmail(我,你,msg.as_string())
s、 退出
这段代码来自python文档
当我运行此代码时,它会连续运行,但不会发送电子邮件
我想知道,除了这段代码,我还需要在其他地方进行一些配置吗
我没有看到任何错误
我正在使用python2.7
这是作为一个解决方案在中给出的。您似乎正在使用gmail id。现在,SMTP服务器不是您的tornado服务器。它是电子邮件提供商的服务器 您可以在线搜索gmail服务器的smtp设置,并获取以下信息:
- 服务器名称:smtp.gmail.com
- SSL的服务器端口:465
- TLS的服务器端口:587
您的代码正试图使用端口5000上本地主机上的SMTP服务器发送电子邮件。@Timothéejeanni抱歉,我对这整件事一无所知。您能告诉我应该怎么做吗?我收到一个
smtplib.SMTPAuthenticationError
您是否使用server.login()
登录了?如果你不登录,gmail会告诉你,你无权从你的帐户发送邮件。事实上,我已经登录了我的gmail帐户。你在哪里登录了你的gmail帐户?如果你在浏览器中登录你的gmail帐户,那就不算了——因为python脚本不知道这一点。python脚本需要给gmail你的密码才能发送电子邮件。我刚刚收到了一封来自gmail的登录尝试。我需要在gmail中设置一些权限吗?我正在正确传递用户名
和密码
。
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"