#!/usr/bin/env python
#-*-coding:utf-8-*-
import socket
import smtplib
import base64
from email.MIMEText import MIMEText
from email import Utils
#用于SSL的smtplib.SMTP的子类,只实现了最基本的功能,有待完善
class SMTP_SSL(smtplib.SMTP):
def __init__(self,host="",port=465,local_hostname=None,key=None,cert=None):
self.cert=cert
self.key=key
smtplib.SMTP.__init__(self,host,port,local_hostname)
def connect(self,host="localhost",port=465):
#if self.debuglevel>0:print >>stderr,"connect: ",(host,port)
self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.sock.connect((host,port))
sslobj=socket.ssl(self.sock,self.key,self.cert)
self.sock=smtplib.SSLFakeSocket(self.sock,sslobj)
self.file=smtplib.SSLFakeFile(sslobj)
msg="getaddrinfo returns an empty list"
code,msg=self.getreply()
if self.debuglevel>0:print >>stderr,"connect:",msg
return (code,msg)
if __name__=="__main__":
fromaddr="test@test.com"#发送端地址可以随便写一个
toaddr="destination"#此处是收件人
message="""Hello!
This is a test message.这是一个测试
"""
msg=MIMEText(message)#这一个程序段是构造一个消息头(header)
msg["To"]=toaddr
msg["From"]=fromaddr
msg["Subject"]="test message"
msg["Date"]=Utils.formatdate(localtime=1)
msg["Message-ID"]=Utils.make_msgid()
username="username"#用于认证的用户名
password="password"#用于认证的密码
host="smtpserver"#用于发送邮件的服务器
smtp=SMTP_SSL(host,port=465,local_hostname=None,key=None,cert=None)#注意端口号是465,因为是SSL连接
smtp.set_debuglevel(1)
smtp.login(username,password)#认证
smtp.sendmail(fromaddr,toaddr,str(msg))
smtp.quit()
#-*-coding:utf-8-*-
import socket
import smtplib
import base64
from email.MIMEText import MIMEText
from email import Utils
#用于SSL的smtplib.SMTP的子类,只实现了最基本的功能,有待完善
class SMTP_SSL(smtplib.SMTP):
def __init__(self,host="",port=465,local_hostname=None,key=None,cert=None):
self.cert=cert
self.key=key
smtplib.SMTP.__init__(self,host,port,local_hostname)
def connect(self,host="localhost",port=465):
#if self.debuglevel>0:print >>stderr,"connect: ",(host,port)
self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.sock.connect((host,port))
sslobj=socket.ssl(self.sock,self.key,self.cert)
self.sock=smtplib.SSLFakeSocket(self.sock,sslobj)
self.file=smtplib.SSLFakeFile(sslobj)
msg="getaddrinfo returns an empty list"
code,msg=self.getreply()
if self.debuglevel>0:print >>stderr,"connect:",msg
return (code,msg)
if __name__=="__main__":
fromaddr="test@test.com"#发送端地址可以随便写一个
toaddr="destination"#此处是收件人
message="""Hello!
This is a test message.这是一个测试
"""
msg=MIMEText(message)#这一个程序段是构造一个消息头(header)
msg["To"]=toaddr
msg["From"]=fromaddr
msg["Subject"]="test message"
msg["Date"]=Utils.formatdate(localtime=1)
msg["Message-ID"]=Utils.make_msgid()
username="username"#用于认证的用户名
password="password"#用于认证的密码
host="smtpserver"#用于发送邮件的服务器
smtp=SMTP_SSL(host,port=465,local_hostname=None,key=None,cert=None)#注意端口号是465,因为是SSL连接
smtp.set_debuglevel(1)
smtp.login(username,password)#认证
smtp.sendmail(fromaddr,toaddr,str(msg))
smtp.quit()