MemoryError:在Python中发送zip文件时内存不足



我有类发送电子邮件。我试图发送一个大约157 mb的zip文件的附件,但我得到一个内存错误。当我发送其他较小的zip文件程序工作正常。有没有人知道如何处理这个问题,所以我可以发送类似的附件,导致问题?

class Sender: 
#constructor
def __init__(self,filename):
    self.filename = filename    
#functions
def message(self):
    fromaddr = "myaddress@gm.com"
    toaddr = ["address"]        
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = ",".join(toaddr)
    msg['Subject'] = "New Base Year"
    # This is the binary part(The Attachment):
    part = MIMEApplication(open('t:\base_year_06_06_2014.zip',"rb").read())
    part.add_header('Content-Disposition', 'attachment', filename='srping2013.zip')
    msg.attach(part)        
    body = "Hello,"
    msg.attach(MIMEText(body, 'plain'))
    server = smtplib.SMTP('c11', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login("myaddress@gm.com", "password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()
    return "Message Sent"

这是当我试图发送zip文件

时得到的错误
Traceback (most recent call last):
File "C:Python27ArcGIS10.1Libsite- packagespythonwinpywinframeworkscriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "T:Python scriptsscriptstestzipper.py", line 58, in <module>
success = send_email.message()
File "T:Python scriptsscriptstestzipper.py", line 36, in message
text = msg.as_string()
File "C:Python27ArcGIS10.1libemailmessage.py", line 137, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:Python27ArcGIS10.1libemailgenerator.py", line 83, in flatten
self._write(msg)
File "C:Python27ArcGIS10.1libemailgenerator.py", line 108, in _write
self._dispatch(msg)
File "C:Python27ArcGIS10.1libemailgenerator.py", line 134, in _dispatch
meth(msg)
File "C:Python27ArcGIS10.1libemailgenerator.py", line 203, in _handle_multipart
g.flatten(part, unixfrom=False)
File "C:Python27ArcGIS10.1libemailgenerator.py", line 83, in flatten
self._write(msg)
File "C:Python27ArcGIS10.1libemailgenerator.py", line 118, in _write
self._fp.write(sfp.getvalue())
MemoryError: out of memory

不幸的是,这完全取决于您的代码分配了多少内存以及您的物理内存限制以及电子邮件服务器上的内存限制。

你可以试着把文件分成几个部分,然后在另一端把它们重新组合在一起,但这并不是一个理想的解决方案。压缩技术也有帮助,但作用很小。

另一个非常复杂的解决方案是,如果您可以访问运行邮件服务器的服务器,您可以临时将附件写入服务器,并在电子邮件中留下指向它的链接,允许用户下载大于服务器本身特定大小的更大的附件。

最新更新