【问题标题】:Getting error `No module named mime.text` [Python]收到错误“没有名为 mime.text 的模块”[Python]
【发布时间】:2017-03-18 23:23:24
【问题描述】:

此代码是“假设”发送电子邮件。

import smtplib
#SERVER = "localhost"

FROM = 'myEmail@email.com'

TO = ["toEmail@email.com"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()

我收到此错误消息。

Traceback (most recent call last):
  File "C:\Users\myCpu\Documents\myFiles\python\test wy.py", line 1, in <module>
    import smtplib
  File "C:\Python27\lib\smtplib.py", line 46, in <module>
    import email.utils
  File "C:/Users/myCpu/Documents/myFiles/python\email.py", line 5, in <module>
ImportError: No module named mime.text

使用:

Python 2.7

Windows 7 专业版

Gmail (@gmail.com)


谁能帮我处理这段代码?

【问题讨论】:

    标签: python python-2.7 email sendmail


    【解决方案1】:

    这就是我为 gmail 所做的。 希望这能解决您的问题

     from email.mime.text import MIMEText
    
        def construct_mesage():
          message = MIMEText(message_text)
          message['to'] = to
          message['from'] = sender
          message['subject'] = subject
          return {'raw': base64.urlsafe_b64encode(message.as_string())}
    

    【讨论】:

    • 我只是运行这个吗?还是需要别的东西??
    • 这给了你一个原始对象,你可以用它来切断
    • 我现在明白了:No module named utils
    • 最后一件事...global name 'base64' is not defined
    【解决方案2】:

    将您正在处理的名为 email.py 的文件重命名为其他名称。它可能会破坏您库中的导入。

    【讨论】:

    • 这是我得到的:server = smtplib.SMTP('myserver') File "C:\Python27\lib\smtplib.py", line 256, in __init__ (code, msg) = self.connect(host, port) File "C:\Python27\lib\smtplib.py", line 316, in connect self.sock = self._get_socket(host, port, self.timeout) File "C:\Python27\lib\smtplib.py", line 291, in _get_socket return socket.create_connection((host, port), timeout) File "C:\Python27\lib\socket.py", line 557, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): gaierror: [Errno 11004] getaddrinfo failed
    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2012-06-13
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多