没有属性'SMTP',尝试发送电子邮件的Python时出错 [英] No attribute 'SMTP', error when trying to send email in Python

查看:1885
本文介绍了没有属性'SMTP',尝试发送电子邮件的Python时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import smtplib 


fromaddr ='......................'
toaddrs ='................ ......'
msg ='垃圾邮件测试'

username ='.......'
password ='...... '

server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
服务器。登录(用户名,密码)
server.sendmail(fromaddr,toaddrs,msg)
server.quit()

我明白这可能不是正确的邮件格式。



无论如何,我收到一个错误:

  C:\ .....> python email.py 
追溯(最近的最后一次呼叫):
文件电子邮件.py,第1行,< module>
import smtplib
文件C:\ ..... \Python\lib\smtplib.py,第47行,
在< module>
import email.utils
文件C:\ ..... \email.py,第15行,
< module>
server = smtplib.SMTP('smtp.gmail.com',587)
AttributeError:'module'对象没有属性'SMTP'

我不太明白我在这里做错了什么?有什么不正确?



注意:所有这些期间都是替换密码/电子邮件/文件路径/ etc。

解决方案

Python已经有一个 电子邮件模块。您的脚本的名称是 email.py ,这是阻止 smtplib 导入内置的电子邮件模块。



将脚本重命名为 email.py 以外的其他脚本问题会消失。


I am trying to send an email in Python:

import smtplib


fromaddr = '......................'  
toaddrs  = '......................'  
msg = 'Spam email Test'  

username = '.......'  
password = '.......'

server = smtplib.SMTP('smtp.gmail.com', 587)  
server.ehlo()
server.starttls()
server.login(username, password)  
server.sendmail(fromaddr, toaddrs, msg)  
server.quit()

I understand that this is probably not the correct message format.

Anyways, I get an error:

C:\.....>python email.py
Traceback (most recent call last):
  File "email.py", line 1, in <module>
    import smtplib
  File "C:\.....\Python\lib\smtplib.py", line 47,
 in <module>
    import email.utils
  File "C:\.....\email.py", line 15, in
<module>
    server = smtplib.SMTP('smtp.gmail.com', 587)
AttributeError: 'module' object has no attribute 'SMTP'

I don't quite understand what I am doing wrong here... Anything incorrect?

NOTE: All the periods are replacements for password/email/file paths/etc.

解决方案

Python already has an email module. Your script's name is email.py, which is preventing smtplib from importing the built-in email module.

Rename your script to something other than email.py and the problem will go away.

这篇关于没有属性'SMTP',尝试发送电子邮件的Python时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆