python模块学习 smtplib 邮件发送
在基于互联网的应用中,程序经常需要自动地发送电子邮件。如:一个网站的注册系统会在用户注册时发送一封邮件来确认注册;当用户忘记登陆密码的时候,通过邮件来取回密码。smtplib模块是python中smtp(简单邮件传输协议)的客户端实现。我们可以使用smtplib模块,轻松的发送电子邮件。下面的例子用了不到十行代码来发送电子邮件:
#coding=gbk
import smtplib
smtp = smtplib.SMTP()
smtp.connect("smtp.yeah.net", "25")
smtp.login('用户名', '密码')
smtp.sendmail('from@yeah.net', 'to@21cn.com', 'from: from@yeah.net\r\nTo: to@21cn.com\r\nSubject: this is a email from python demo\r\n\r\nJust for test~_~')
smtp.quit()
#coding=gbk
import smtplib
smtp = smtplib.SMTP()
smtp.connect("smtp.yeah.net", "25")
smtp.login('用户名', '密码')
smtp.sendmail('from@yeah.net', 'to@21cn.com', 'from: from@yeah.net\r\nTo: to@21cn.com\r\nSubject: this is a email from python demo\r\n\r\nJust for test~_~')
smtp.quit()
这个例子够简单吧^_^!下面详细介绍stmplib模块中的类和方法。
smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])
SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接我们可以向smtp服务器发送指令,执行相关操作(如:登陆、发送邮件)。该类提供了许多方法,将在下面介绍。它的所有参数都是可选的,其中host参数表示smtp服务器主机名,上面例子中的smtp主机为"smtp.yeah.net";port表示smtp服务的端口,默认是25;如果在创建SMTP对象的时候提供了这两个参数,在初始化的时候会自动调用connect方法去连接服务器。
smtplib模块还提供了SMTP_SSL类和LMTP类,对它们的操作与SMTP基本一致。
smtplib.SMTP提供的方法:
SMTP.set_debuglevel(level)
设置是否为调试模式。默认为False,即非调试模式,表示不输出任何调试信息。
SMTP.connect([host[, port]])
连接到指定的smtp服务器。参数分别表示smpt主机和端口。注意: 也可以在host参数中指定端口号(如:smpt.yeah.net:25),这样就没必要给出port参数。
SMTP.docmd(cmd[, argstring])
向smtp服务器发送指令。可选参数argstring表示指令的参数
相关文档:
运行一句python命令
对vc设置路径
include:D:\PYTHON31\INCLUDE
lib:D:\PYTHON31\LIBS
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
Py_Initialize() ;
PyRun_SimpleString("print('Hello')");
//PyRun_SimpleString("print(dir())");
Py_Finalize();& ......
# -*- coding: cp936 -*-
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import smtplib
#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件
att = MIMEText(open('e:\\test.txt').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/ ......
本篇将介绍python中sys, getopt模块处理命令行参数
如果想对python脚本传参数,python中对应的argc, argv(c语言的命令行参数)是什么呢?
需要模块:sys
参数个数:len(sys.argv)
脚本名: sys.argv[0]
参数1: sys.argv[1]
参数2: sys.argv[2]
test.py
1
import ......
使用 Python 分离中文与英文的混合字串
LiYanrui
posted @ 大约 1 年前
in 程序设计
with tags
python
, 614 阅读
这个问题是做 MkIV 预处理程序
时搞定的,就是把一个混合了中英文混合字串分离为英文与中文的子字串,譬如,将 ”我的 English 学的不好
&ld ......
from http://stackoverflow.com/questions/101268/hidden-features-of-python
http://www.okpython.com/bbs/thread-3434-1-1.html
http://www.okpython.com/bbs/thread-3436-1-2.html
http://www.okpython.com/bbs/thread-3437-1-2.html
http://www.okpython.com/bbs/thread-3438-1-1.html
http://www.okpython.com/bb ......