无法从蟒蛇
我使用下面的代码从Python程序在本地主机发送电子邮件发送电子邮件,无法从蟒蛇
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "[email protected]"
you = "[email protected]"
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()
此代码是从Python文档。
当我运行此代码时,它连续运行,但没有发送电子邮件。
我想知道,除了这段代码外,我还必须在其他地方做一些其他配置。
我没有看到任何错误。
我使用python 2.7
的解决方案看来你使用的是Gmail ID。现在,SMTP服务器不是你的龙卷风服务器。它是电子邮件提供商的服务器。
您可以为Gmail服务器的SMTP设置在线搜索,得到如下:
- 服务器名称:smtp.gmail.com用于SSL
- 服务器端口:465
- 服务器端口TLS:587
我从http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm
而且得到他们,您需要确保在执行此操作时不启用gmail的2步身份验证,否则会失败。此外,Gmail特别可能会要求您发送其他内容,例如ehlo和starttls。你可以找到一个完整的例子这里以前的答案:How to send an email with Gmail as provider using Python?
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"
我得到一个'smtplib.SMTPAuthenticationError' –
你有登录使用'server.login()'?如果您不登录,Gmail会告诉您您未经授权从您的帐户发送邮件。 – AbdealiJK
其实我已经登录到我的Gmail账户 –
你的代码试图利用SMTP服务器在本地主机端口5000 –
@TimothéeJeannin上发送电子邮件对不起,我是新来这整个事情能你告诉我我该怎么做? –