1

这是我从来源复制并尝试学习的简单电子邮件发送代码。但即使我安装了 easy_install 电子邮件,这也会出错。下面是代码:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sendmail (from_email, password, to_email, subject, message):
    msg=MIMEMultipart()
    msg['From']= from_email
    msg['To']= to_email
    msg['Subject']= subject

    msg.attach(MIMEText(message, 'plain'))
    try:
        server= smtplib.SMTP_SSL('smtp.office365.com', 465)
        server.echo()
        server.login(from_email, password)
        server.sendmail(from_email, to_email, msg.as_string())
        server.close()
        return  True
    except Exception as e:
        print('Something went wrong '+str())
        return False

Traceback (most recent call last):
  File "C:\Users\RYadav\PycharmProjects\MAPILab\rough work.py", line 1, in <module>
    import smtplib
  File "C:\Users\RYadav\PycharmProjects\MAPILab\smtplib.py", line 2, in <module>
    from email.MIMEMultipart import MIMEMultipart
ModuleNotFoundError: No module named 'email.MIMEMultipart'

Process finished with exit code 1

4

1 回答 1

0

使用这个命令:


从 email.mime.multipart 导入 MIMEMultipart

从 email.mime.text 导入 MIMEText


检查这个留置权= https://docs.python.org/3/library/email.mime.html#module-email.mime

于 2021-01-17T21:00:55.210 回答