def main(): # 请自行修改下面的邮件发送者和接收者 sender = '*****@*****.**' receivers = ['*****@*****.**', '*****@*****.**'] message = MIMEText('用Python发送邮件的示例代码.', 'plain', 'utf-8') message['From'] = Header('邵朋', 'utf-8') #发件人 message['To'] = Header('鹏少', 'utf-8') #收件人 message['Subject'] = Header('示例代码试验邮件', 'utf-8') #邮件内容 smtper = SMTP('smtp.163.com') #使用smtp邮件协议 # 请自行修改下面的登录口令 smtper.login(sender, 'shao1990PENG') #邮箱登录密码 smtper.sendmail(sender, receivers, message.as_string()) print('邮件发送完成!')
def main(): # 创建一个带附件的邮件消息对象 message = MIMEMultipart() # 创建文本内容 text_content = MIMEText('附件中有本月数据请查收', 'plain', 'utf-8') message['Subject'] = Header('本月数据', 'utf-8') # 将文本内容添加到邮件消息对象中 message.attach(text_content) # 读取文件并将文件作为附件添加到邮件消息对象中 # python读取windows系统文件要么写全路径,要么绝对路径要么相对路径 with open('/Uscers/Administrator/Desktop/hello', 'rb') as f: txt = MIMEText(f.read(), 'base64', 'utf-8') txt['Content-Type'] = 'text/plain' txt['Content-Disposition'] = 'attachment; filename=hello.txt' message.attach(txt) # 读取文件并将文件作为附件添加到邮件消息对象中 with open('/Usecrs/Administrator/Desktop/媒体版故障处理.docx', 'rb') as f: xls = MIMEText(f.read(), 'base64', 'utf-8') xls['Content-Type'] = 'application/vnd.ms-doc' # 或 application/vnd.ms-excel xls['Content-Disposition'] = 'attachment; filename=month-data.doc' #重命名为month-data.xlsx message.attach(xls) # 创建SMTP对象 smtper = SMTP('smtp.163.com') # 开启安全连接 # smtper.starttls() sender = '*****@*****.**' receivers = ['*****@*****.**'] # 登录到SMTP服务器 # 请注意此处不是使用密码而是邮件客户端授权码进行登录 # 对此有疑问的读者可以联系自己使用的邮件服务器客服 smtper.login(sender, 'shao1990PENG') # 发送邮件 smtper.sendmail(sender, receivers, message.as_string()) # 与邮件服务器断开连接 smtper.quit() print('发送完成!')