您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

python使用smtplib模块通过gmail实现邮件发送的方法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   这篇文章主要介绍了python使用smtplib模块通过gmail实现邮件发送的方法,涉及Python使用smtplib模块发送邮件的相关技巧,非常简单实用,需要的朋友可以参考下

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText fromaddr = '[email protected]' toaddr = '[email protected]' text = 'test email message sent from Python code' username = 'fromaddruser' password = 'fromaddrpassword' msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = 'Test' msg.attach(MIMEText(text)) server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.sendmail(fromaddr, toaddr, msg.as_string()) server.quit()

  希望本文所述对大家的Python程序设计有所帮助。