从python发送电子邮件的解决方案

时间:2017-04-09 19:07:29

标签: python gmail

这是使用python

发送gmail的解决方案

导入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

0 个答案:

没有答案