Home  >  Q&A  >  body text

Using python3's smtplib library to send emails keeps returning the message that the email cannot be sent. What is the reason?

According to the online tutorial for sending emails in python3, when running in jupyter, a message of failure to send is returned. Is there any missing environment?

The code should be fine, but I’ll post it anyway:

# import smtplib
# from email.mime.text import MIMEText
# from email.header import Header

# # 第三方 SMTP 服务
# mail_host="smtp.qq.com"  #设置服务器
# mail_user="我的邮箱"    #用户名
# mail_pass="邮箱口令"   #口令 

# sender = '我的邮箱'
# receivers = ['目标邮箱']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
 
# # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
# message = MIMEText('这是一个礼物,我等会会删掉的', 'plain', 'utf-8')
# message['From'] = Header("你的邮件", 'utf-8')
# message['To'] =  Header("你的邮件", 'utf-8')

# subject = '你的邮件'
# message['Subject'] = Header(subject, 'utf-8')

# try:
#     smtpObj = smtplib.SMTP("smtp.qq.com") 
#     smtpObj.connect("smtp.qq.com", 25)    # 25 为 SMTP 端口号
#     smtpObj.login(mail_user,mail_pass)
#     smtpObj.sendmail(sender, receivers, message.as_string())
#     print ("邮件发送成功")
# except smtplib.SMTPException:
#     print("Error: 无法发送邮件")
三叔三叔2686 days ago966

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-06-12 09:27:11

    Your email account is enabled POP3/SMTP/IMAP Are these protocols

    reply
    0
  • 代言

    代言2017-06-12 09:27:11

    1. Understand the connections and differences between SMTP, IMAP, and POP3, understand how email is sent/received, and understand what MIME is all about

    2. Find out whether your email service provider has something like App Code (all major mailboxes have it)

    3. Don’t use the Python standard library to do this, for example: https://github.com/hezhiming/...

    Actually, as long as you get 1, everything else is just a cloud

    reply
    0
  • Cancelreply