如何从蟒蛇
我的代码如何从蟒蛇
import smtplib
import socket
import sys
from email.mime.text import MIMEText
fp = open("CR_new.txt", 'r')
msg = MIMEText(fp.read())
fp.close()
you = "[email protected]"
me = "[email protected]"
msg['Subject'] = 'The contents of %s' % "CR_new.txt"
msg['From'] = you
msg['To'] = me
s = smtplib.SMTP('127.0.0.1')
s.sendmail(you,me, msg.as_string())
s.quit()
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
注发送电子邮件:
- 没有一个SMTP服务器
此代码将帮助您发送电子邮件。 你只需要提供你的电子邮件密码。
最重要的注意事项是:不要将文件名称作为email.py。
import socket
import sys
import smtplib
EMAIL_TO = ["[email protected]"]
EMAIL_FROM = "[email protected]"
EMAIL_SUBJECT = "Test Mail... "
msg= {}
EMAIL_SPACE = ", "
msg['Subject'] = EMAIL_SUBJECT
msg['To'] = EMAIL_SPACE.join(EMAIL_TO)
msg['From'] = EMAIL_FROM
try:
mail = smtplib.SMTP_SSL('smtp.bizmail.yahoo.com',465)
#
# if you are using gmail then use
# smtplib.SMTP_SSL('smtp.gmail.com',587)
#
mail.login("[email protected]", 'password') # you account email password..
mail.sendmail("[email protected]", EMAIL_TO, msg.as_string())
mail.quit()
except Exception,e:
print e
finally:
print "Error of email sending mail"
如何使用本地主机发送电子邮件? – RajivKumar
可能是你需要一个电子邮件服务器,因为你不能从本地主机发送它。 –
我建议使用像yagmail这样的包,而不是试图弄清楚如何让smtplib工作。免责声明:我是yagmail的维护者。
代码是这样:
import yagmail
yag = yagmail.SMTP(host="127.0.0.1")
yag.send(to"[email protected]", subject="subject", contents="content")
with open(os.path.expanduser(“〜/ .yagmail”))as f: FileNotFoundError:[Errno 2]没有这样的文件或目录:'C:\\ Users \\ rajivkum/.yagmail' >> >我遇到了yagmail的这个错误 – RajivKumar
正如你注意的那样,没有密码和用户名:-)运行一次'yagmail.register(“username”,“password”)''它会将你的密码保存在你的操作系统密钥环中。在用户文件夹中的.yagmail文件中使用您的用户名将允许无密码脚本。或者:'yagmail.SMTP(“username”,“password”)'如果你喜欢在脚本中写入用户名/密码。 – PascalVKooten
在你的情况下,你会把'.yagmail'放到你的'C:\ Users \ rajivkum'文件夹中。 – PascalVKooten
你有一个SMTP服务器上的本地主机上运行? –
我不这么认为,我正在使用Windows 7 – RajivKumar
然后你需要一个SMTP服务器(你自己或任何其他你有一个帐户)。 –