2016-11-05 124 views
1

此代碼是「假設」發送電子郵件。獲取錯誤`No module named mime.text` [Python]

import smtplib 
#SERVER = "localhost" 

FROM = '[email protected]' 

TO = ["[email protected]"] # 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的Professionnal

的Gmail(@ gmail.com)


誰能幫我個是代碼?

回答

3

重命名您正在使用的命名email.py到別的文件。這很可能會破壞圖書館的進口。

+0

這是我得到的:'server = smtplib.SMTP('myserver') 文件「C:\ Python27 \ lib \ smtplib.py」,第256行,在__init__中 (code,msg)= self。連接(主機,端口) 文件「C:\ Python27 \ lib \ smtplib.py」,行316,連接 self.sock = self._get_socket(host,port,self.timeout) 文件「C:\ Python27 \ lib \ smtplib.py「,第291行,在_get_socket 返回socket.create_connection((主機,端口),超時) 文件」C:\ Python27 \ lib \ socket.py「,第557行,在create_connection中 在getaddrinfo(主機,端口,0,SOCK_STREAM)中: gaierror:[Errno 11004] getaddrinfo失敗' –

2

這就是我爲gmail所做的。 希望這sloves您的問題

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())} 
+0

我只是運行這個?或者是其他需要的東西? –

+0

這給你一個原始對象,你可以這樣切斷 –

+0

我現在得到這個:'沒有命名爲utils模塊# –