使用python中的yahoo帐户发送电子邮件

use*_*194 6 python email smtp

我有yahoo账号.是否有任何python代码可以从我的帐户发送电子邮件?

use*_*472 12

是的 这是代码:

import smtplib
fromMy = 'yourMail@yahoo.com' # fun-fact: from is a keyword in python, you can't use it as variable, did abyone check if this code even works?
to  = 'SomeOne@Example.com'
subj='TheSubject'
date='2/1/2010'
message_text='Hello Or any thing you want to send'

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )

username = str('yourMail@yahoo.com')  
password = str('yourPassWord')  

try :
    server = smtplib.SMTP("smtp.mail.yahoo.com",587)
    server.login(username,password)
    server.sendmail(fromMy, to,msg)
    server.quit()    
    print 'ok the email has sent '
except :
    print 'can\'t send the Email'
Run Code Online (Sandbox Code Playgroud)

  • 应该在使用server.login之前添加server.starttls(),否则会引发异常. (4认同)
  • `服务器不支持SMTP AUTH扩展名 (3认同)
  • 您必须在设置中启用应用程序身份验证才能执行此操作。 (2认同)

xpr*_*ros 5

关于使用雅虎的smtp服务器,我(简要地)绞尽脑汁.465就行不通.我决定通过587端口的TLS路由,我能够进行身份验证和发送电子邮件.

import smtplib
from email.mime.text import MIMEText
SMTP_SERVER = "smtp.mail.yahoo.com"
SMTP_PORT = 587
SMTP_USERNAME = "username"
SMTP_PASSWORD = "password"
EMAIL_FROM = "fromaddress@yahoo.com"
EMAIL_TO = "toaddress@gmail.com"
EMAIL_SUBJECT = "REMINDER:"
co_msg = """
Hello, [username]! Just wanted to send a friendly appointment
reminder for your appointment:
[Company]
Where: [companyAddress]
Time: [appointmentTime]
Company URL: [companyUrl]
Change appointment?? Add Service??
change notification preference (text msg/email)
"""
def send_email():
    msg = MIMEText(co_msg)
    msg['Subject'] = EMAIL_SUBJECT + "Company - Service at appointmentTime"
    msg['From'] = EMAIL_FROM 
    msg['To'] = EMAIL_TO
    debuglevel = True
    mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    mail.set_debuglevel(debuglevel)
    mail.starttls()
    mail.login(SMTP_USERNAME, SMTP_PASSWORD)
    mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
    mail.quit()

if __name__=='__main__':
send_email()
Run Code Online (Sandbox Code Playgroud)

  • 我收到错误:`smtplib.SMTPServerDisconnected:连接意外关闭`但我已经通过转到我的雅虎“帐户安全”设置并选中“允许使用不太安全的登录的应用程序”选项解决了这个问题 (3认同)

小智 5

在此访问雅虎帐户安全页面

您需要生成一个应用程序密码 - 这是屏幕底部的一个选项。在您的脚本中使用此页面上生成的 Yahoo 密码。