1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| def sead_email(): #发送邮件函数 server = smtplib.SMTP("smtp.gmail.com", 587) #创建和gmail服务器的连接 server.starttls() # 开始连接 server.login(accout, password) # 进行登录,这里的password不是账号的密码,而是google二次验证给的一个代码 # read a email # Send an email print('Now i will send a email to ' + destination)
message = MIMEText('Python script', 'plain', 'utf-8') # 邮件内容 message['From'] = Header("Python send email bot", 'utf-8') # 发送者
subject = 'Python SMTP 邮件测试' # 邮件主题 message['Subject'] = Header(subject, 'utf-8') # 邮件主题
server.sendmail(accout, destination, message.as_string()) # Close the connection server.quit() #断开连接 print('Email sent!')
|