Python 3.6-从Python发送电子邮件
我正在尝试使用python发送一封电子邮件,带有附件。这是我的密码:Python 3.6-从Python发送电子邮件,python,email,Python,Email,我正在尝试使用python发送一封电子邮件,带有附件。这是我的密码: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders fromaddr = "name@company.com" toaddr = "name2@compa
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "name@company.com"
toaddr = "name2@company.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Testing Python Email - Plus Attachments"
body = "This is an automated email"
msg.attach(MIMEText(body, 'plain'))
filename = "nice.png"
attachment = open("C:\\Users\\Ben Hannah\\Documents", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.office365.com', 587)
server.starttls()
server.login(fromaddr, "************")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
这就是它的回报:
Traceback (most recent call last):
File "C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\base64.py", line 517, in _input_type_check
m = memoryview(s)
TypeError: memoryview: a bytes-like object is required, not 'NoneType'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\email\encoders.py", line 32, in encode_base64
encdata = str(_bencode(orig), 'ascii')
File "C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\base64.py", line 534, in encodebytes
_input_type_check(s)
File "C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\base64.py", line 520, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not NoneType
回溯(最近一次呼叫最后一次):
文件“C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\base64.py”,第517行,输入类型检查
m=记忆视图(s)
TypeError:memoryview:需要类似字节的对象,而不是“NoneType”
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\email\encoders.py”,第32行,在encode\u base64中
encdata=str(_bencode(orig),‘ascii’)
文件“C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\base64.py”,第534行,以字节为单位
_输入类型检查
文件“C:\Users\Ben Hannah\AppData\Local\Programs\Python\Python36-32\lib\base64.py”,第520行,输入类型检查
从错误中引发类型错误(msg)
TypeError:应为类似对象的字节,而不是非非类型
电子邮件发送,邮箱接收,看到的是一个附件-但当试图打开它时,它说它无法打开文件 您忘记在
open
中包含文件名
filename = "nice.png"
attachment = open("C:\\Users\\Ben Hannah\\Documents", "rb")
试试看
您忘记在
open
中包含文件名
filename = "nice.png"
attachment = open("C:\\Users\\Ben Hannah\\Documents", "rb")
试试看
它能在没有附件的情况下工作吗?它能在没有附件的情况下工作吗?嗨!这很有道理,谢谢你!你能告诉我如何让这封邮件每小时自动运行吗?我会使用循环还是递归?我会使用一些调度工具。这里有一些windows选项:嗨!这很有道理,谢谢你!你能告诉我如何让这封邮件每小时自动运行吗?我会使用循环还是递归?我会使用一些调度工具。这里有一些适用于windows的选项: