用Python发送邮件
来源:互联网 发布:网络营销软件168108cc 编辑:程序博客网 时间:2024/04/20 00:23
服务器需要周期性的检查邮件队列,然后通过smtp服务器发送出去,就网上搜了搜,然后修改了加工了一下,写了一个邮件发送的简单模块
两个文件
config.py:配置信息
send_msg.py:发送邮件
send_msg.py
config.py
=====================================
btw: csdn blog太让人伤心了,贴点代码怎么着都贴不上来:!
两个文件
config.py:配置信息
send_msg.py:发送邮件
send_msg.py
#coding=utf-8
import smtplib,config,email,sys
from email.Message import Message
def connect():
"connect to smtp server and return a smtplib.SMTP instance object"
server=smtplib.SMTP(config.smtpserver,config.smtpport)
server.ehlo()
server.login(config.smtpuser,config.smtppass)
return server
def sendmessage(server,to,subj,content):
"using server send a email"
msg = Message()
msg['Mime-Version']='1.0'
msg['From'] = config.smtpuser
msg['To'] = to
msg['Subject'] = subj
msg['Date'] = email.Utils.formatdate() # curr datetime, rfc2822
msg.set_payload(content)
try:
failed = server.sendmail(config.smtpuser,to,str(msg)) # may also raise exc
except Exception ,ex:
print Exception,ex
print 'Error - send failed'
else:
print "send success!"
if __name__=="__main__":
#frm=raw_input('From? ').strip()
to=raw_input('To? ').strip()
subj=raw_input('Subj? ').strip()
print 'Type message text, end with line="."'
text = ''
while True:
line = sys.stdin.readline()
if line == '. ': break
text += line
server=connect()
sendmessage(server,to,subj,text)
import smtplib,config,email,sys
from email.Message import Message
def connect():
"connect to smtp server and return a smtplib.SMTP instance object"
server=smtplib.SMTP(config.smtpserver,config.smtpport)
server.ehlo()
server.login(config.smtpuser,config.smtppass)
return server
def sendmessage(server,to,subj,content):
"using server send a email"
msg = Message()
msg['Mime-Version']='1.0'
msg['From'] = config.smtpuser
msg['To'] = to
msg['Subject'] = subj
msg['Date'] = email.Utils.formatdate() # curr datetime, rfc2822
msg.set_payload(content)
try:
failed = server.sendmail(config.smtpuser,to,str(msg)) # may also raise exc
except Exception ,ex:
print Exception,ex
print 'Error - send failed'
else:
print "send success!"
if __name__=="__main__":
#frm=raw_input('From? ').strip()
to=raw_input('To? ').strip()
subj=raw_input('Subj? ').strip()
print 'Type message text, end with line="."'
text = ''
while True:
line = sys.stdin.readline()
if line == '. ': break
text += line
server=connect()
sendmessage(server,to,subj,text)
config.py
=====================================
smtpserver='mail.xxxx.net'
smtpuser='user@xxx.net'
smtppass='pwd'
smtpport='25'
=====================================smtpuser='user@xxx.net'
smtppass='pwd'
smtpport='25'
btw: csdn blog太让人伤心了,贴点代码怎么着都贴不上来:!
- 用Python发送邮件
- 用Python发送邮件
- 用Python发送邮件
- 用Python发送邮件
- 用python发送邮件
- 用Python来发送邮件
- 用python发送邮件 smtp
- 用Python实现SMTP邮件发送
- Python 发送邮件
- python 邮件发送
- 使用PYTHON发送邮件
- python邮件发送
- python发送邮件程序
- python: 发送邮件
- python发送邮件
- python smtp发送邮件
- python 发送邮件实例
- python发送各类邮件
- SqlDataSource Web 服务器控件
- 学黑客需要什么基础
- Transact-SQL 参考 全文检索
- 赢在中国视频下载
- javamail的getContent()抛出UnsupportedEncodingException的解决方法
- 用Python发送邮件
- 学期总结
- Windows常用命令集
- 对CodeSmith + netTiers 生成DAL的一点补充
- 虚拟机(linux OR win)与主机(windows)共享文件的N种方法总结
- 使用GDB
- Windows网络命令行大全
- sql server锁机制
- 筷子