python发送qq邮件
import smtplib from email.mime.text import MIMEText # email 用于构建邮件内容 from email.header import Header from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase # 用于构建邮件头 from email import encoders # 发信方的信息:发信邮箱,QQ 邮箱授权码 from_addr = 'xx@qq.com' password = 'hjfjgjkglkj' # 收信方邮箱 to_addr = '77@qq.com' # 发信服务器 smtp_server = 'smtp.qq.com' message = MIMEMultipart() message.attach(MIMEText("我发送邮件了!嘿嘿!", 'plain', 'utf-8')) # 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码 # msg = MIMEText('send by python', 'plain', 'utf-8') # 邮件头信息 message['From'] = from_addr message['To'] = Header(to_addr) message['Subject'] = Header('老子来了!') # 添加文件到附件 with open('D:\code\客户\俄尔浦斯的琴弦_EDGAR\data.xlsx', 'rb') as f: # MIMEBase表示附件的对象 mime = MIMEBase('text', 'txt', filename='D:\code\客户\俄尔浦斯的琴弦_EDGAR\data.xlsx') # filename是显示附件名字 mime.add_header('Content-Disposition', 'attachment', filename="data.xlsx") # 获取附件内容 mime.set_payload(f.read()) encoders.encode_base64(mime) # 作为附件添加到邮件 message.attach(mime) # 开启发信服务,这里使用的是加密传输 server = smtplib.SMTP_SSL() server.connect(smtp_server, 465) # 登录发信邮箱 server.login(from_addr, password) # 发送邮件 server.sendmail(from_addr, to_addr, message.as_string()) # 关闭服务器