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

yipeiwu_com6年前Python基础

本文实例讲述了python使用smtplib模块通过gmail实现邮件发送的方法。分享给大家供大家参考。具体实现方法如下:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = 'fromaddr@gmail.com'
toaddr = 'toaddr@gmail.com'
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程序设计有所帮助。

相关文章

用Python的线程来解决生产者消费问题的示例

我们将使用Python线程来解决Python中的生产者—消费者问题。这个问题完全不像他们在学校中说的那么难。 如果你对生产者—消费者问题有了解,看这篇博客会更有意义。 为什么要关心生产者...

python async with和async for的使用

网上async with和async for的中文资料比较少,我把PEP 492中的官方陈述翻译一下。 异步上下文管理器”async with” 异步上下文管理器指的是在enter和e...

python实现数据写入excel表格

本文实例为大家分享了python数据写入excel表格的具体代码,供大家参考,具体内容如下 安装: xlsxwriter第三方库 code: #!/usr/bin/env/pytho...

利用anaconda保证64位和32位的python共存

背景 喵哥想在MFC中调用python脚本,在原来的代码中包含一个只支持x86的库文件(超级核心的文件),原本安装的python是x64的,强行运行程序会出现python头文件里的函数无...

Python 获得命令行参数的方法(推荐)

本篇将介绍python中sys, getopt模块处理命令行参数 如果想对python脚本传参数,python中对应的argc, argv(c语言的命令行参数)是什么呢? 需要模块:sy...