如何在python中发送电子邮件

时间:2023-01-05 16:20:49
import smtplib

SERVER = "localhost"

FROM = "sender@example.com"
TO = ["user@example.com"]

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

This is giving the error:

这给出了错误:

'**The debugged program raised the exception unhandled AttributeError
"'module' object has no attribute 'SMTP'"
File: /home/an/Desktop/email.py, Line: 13**'

2 个解决方案

#1


Rename your file to something other than email.py. Also get rid of any email.pyc file left over. Problem solved.

将您的文件重命名为email.py以外的其他内容。还要删掉任何遗留下来的email.pyc文件。问题解决了。

#2


This happens because email is a built-in library that comes standard with python. If you rename your program to something else (as suggested above), that should do the trick.

发生这种情况是因为电子邮件是python标准的内置库。如果你将程序重命名为其他东西(如上所述),那应该可以解决问题。

#1


Rename your file to something other than email.py. Also get rid of any email.pyc file left over. Problem solved.

将您的文件重命名为email.py以外的其他内容。还要删掉任何遗留下来的email.pyc文件。问题解决了。

#2


This happens because email is a built-in library that comes standard with python. If you rename your program to something else (as suggested above), that should do the trick.

发生这种情况是因为电子邮件是python标准的内置库。如果你将程序重命名为其他东西(如上所述),那应该可以解决问题。