当我尝试安装easy\u install email时,如何解决没有名为“email.MIMEMultipart”的模块的问题?

2024-03-29 08:03:38 发布

您现在位置:Python中文网/ 问答频道 /正文

This is the simple email sending code I have copied from a source and trying to learn it. But this is giving error even after I have installed easy_install email. Below is the code :

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

Tags: thetofromimportserverisemailhave