no module named 'email' 10
importemail.utilsfromemailimportencodersfromemail.headerimportHeaderfromemail.mime.ba...
import email.utils
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase
f
报错信息为:
Traceback (most recent call last):
File "sendyoujian_20170222.py", line 7, in <module>
import email.utils
ImportError: No module named 'email'
rom email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.utils import parseaddr, formataddr, formatdate
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr(Header(name, 'utf-8').encode(), addr)
from_addr = input('From: ')
password = input('Password: ')
to_addr = input('To: ')
smtp_server = input('SMTP server:')
#msg = MIMEText('hello, send by python', 'plain', 'utf-8')
"""带附件的邮件可以看做包含若干部分的邮件:文本和各个附件本身,所以,可以构造一个
MIMEMultipart对象代表邮件本身,然后往里面加上一个MIMEText作为邮件正文,再继续往
里面加上表示附件的MIMEBase对象即可:"""
#邮件对象
msg = MIMEMultipart()
msg['From'] = _format_addr('Python爱好者<%s>'% from_addr)
#msg['To']接收的是字符串而不是list,如果有多个邮件地址,用逗号(,)分隔
msg['To'] = _format_addr('管理员<%s>'% to_addr)
msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
with open('/User/michael/Downloads/test.png', 'rb') as f:
mime = MIMEBase('image','png', filename = 'test.png')
mime.add_header('Content-Disposition','attachment', filename='test.png')
mime.add_header('Content-ID','<0>')
mime.add_header('X-Attachment-Id','0')
mime.set_payload(f.read())
encoders.encode_base64(mine)
msg.attach(mine)
server = smtplib.SMTP(smtp_server, 25)
#set_debuglevel(1)
server.set_debuglevel(1)
#login方法用来登录SMTP服务器
server.login(from_addr, password)
server.sendmail(from_addr,[to_addr], msg.as_string())
server.quit() 展开
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase
f
报错信息为:
Traceback (most recent call last):
File "sendyoujian_20170222.py", line 7, in <module>
import email.utils
ImportError: No module named 'email'
rom email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.utils import parseaddr, formataddr, formatdate
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr(Header(name, 'utf-8').encode(), addr)
from_addr = input('From: ')
password = input('Password: ')
to_addr = input('To: ')
smtp_server = input('SMTP server:')
#msg = MIMEText('hello, send by python', 'plain', 'utf-8')
"""带附件的邮件可以看做包含若干部分的邮件:文本和各个附件本身,所以,可以构造一个
MIMEMultipart对象代表邮件本身,然后往里面加上一个MIMEText作为邮件正文,再继续往
里面加上表示附件的MIMEBase对象即可:"""
#邮件对象
msg = MIMEMultipart()
msg['From'] = _format_addr('Python爱好者<%s>'% from_addr)
#msg['To']接收的是字符串而不是list,如果有多个邮件地址,用逗号(,)分隔
msg['To'] = _format_addr('管理员<%s>'% to_addr)
msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
with open('/User/michael/Downloads/test.png', 'rb') as f:
mime = MIMEBase('image','png', filename = 'test.png')
mime.add_header('Content-Disposition','attachment', filename='test.png')
mime.add_header('Content-ID','<0>')
mime.add_header('X-Attachment-Id','0')
mime.set_payload(f.read())
encoders.encode_base64(mine)
msg.attach(mine)
server = smtplib.SMTP(smtp_server, 25)
#set_debuglevel(1)
server.set_debuglevel(1)
#login方法用来登录SMTP服务器
server.login(from_addr, password)
server.sendmail(from_addr,[to_addr], msg.as_string())
server.quit() 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询