导入smtplib 来自email.mime.multipart导入MIMEMultipart 来自email.mime.text导入MIMEText
msg = MIMEMultipart()
msg['From'] = 'me@gmail.com'
msg['To'] = 'you@gmail.com'
msg['Subject'] = 'Hello'
message = 'This is mail from python.'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('me@gmail.com', 'password')
mailserver.sendmail('me@gmail.com','you@gmail.com',msg.as_string())
mailserver.quit()
# Note: You may have to change following settings
# disable access protection. to do this right click mcafee virus then
#open
#virus scan control and right click access protection
# in gmail allow less secure apps
#https://myaccount.google.com/security#connectedapps