Last active
August 2, 2016 02:55
-
-
Save dengshilong/4266409 to your computer and use it in GitHub Desktop.
使用smtplib的例子
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--*-- encoding:utf-8 --*-- | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.base import MIMEBase | |
from email import encoders | |
from datetime import date | |
def sendMail(user,pwd,to,subject,filename): | |
outer = MIMEMultipart() | |
outer['From'] = user | |
outer['To'] = to | |
outer['Subject'] = subject | |
fp = open(filename, 'rb') | |
msg = MIMEBase('application', 'octet-stream') | |
msg.set_payload(fp.read()) | |
fp.close() | |
encoders.encode_base64(msg) | |
msg.add_header('Content-Disposition', 'attachment', filename=filename) | |
outer.attach(msg) | |
try: | |
smtpServer = smtplib.SMTP('smtp.163.com',25) #邮件服务器,根据所用邮箱设定 | |
print "Connecting To Mail Server." | |
smtpServer.ehlo() | |
print "logging Into Mail Server." | |
smtpServer.login(user,pwd) | |
print "sending mail" | |
smtpServer.sendmail(user,to,outer.as_string()) | |
smtpServer.quit() | |
print "send success" | |
except Exception,e: | |
print str(e) | |
return False | |
if __name__ == "__main__": | |
user = ''#邮件地址 | |
password = ''#密码 | |
to = ""#收件人 | |
subject = u"今天风云榜的抓取结果" | |
today = date.today().isoformat() | |
filename = 'result' + today + '.csv' | |
sendMail(user, password, to, subject, filename) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--*-- encoding:utf-8 --*-- | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.image import MIMEImage | |
def sendMail(user,pwd,to,subject,filename): | |
outer = MIMEMultipart() | |
outer['From'] = user | |
outer['To'] = to | |
outer['Subject'] = subject | |
fp = open(filename, 'rb') | |
msg = MIMEImage(fp.read()) | |
msg.add_header('Content-Disposition', 'attachment', filename=filename) | |
fp.close() | |
outer.attach(msg) | |
try: | |
smtpServer = smtplib.SMTP('smtp.163.com', 25)#邮件服务器,根据所用邮箱设定 | |
print "Connecting To Mail Server." | |
smtpServer.ehlo() | |
print "logging Into Mail Server." | |
smtpServer.login(user, pwd) | |
print "sending mail" | |
smtpServer.sendmail(user, to, outer.as_string()) | |
smtpServer.quit() | |
print "send success" | |
except Exception,e: | |
print str(e) | |
return False | |
if __name__ == "__main__": | |
user = "" #邮件地址 | |
password = "" #密码 | |
to = "" #收件人 | |
subject = u"哈哈,无聊一把" #标题 | |
filename = "tazuya.jpg" #文件名 | |
sendMail(user, password, to, subject, filename) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--*-- encoding:utf-8 --*-- | |
import smtplib | |
from email.mime.text import MIMEText | |
def sendMail(user,pwd,to,subject,text): | |
msg = MIMEText(text,_charset='utf-8')#添加编码,否则会出现乱码 | |
msg['From'] = user | |
msg['To'] = to | |
msg['Subject'] = subject | |
try: | |
smtpServer = smtplib.SMTP('smtp.163.com',25) #邮件服务器,根据所用邮箱设定 | |
print "Connecting To Mail Server." | |
smtpServer.ehlo() | |
print "starting encrypted session." | |
smtpServer.starttls() | |
smtpServer.ehlo() | |
print "logging Into Mail Server." | |
smtpServer.login(user,pwd) | |
print "sending mail" | |
smtpServer.sendmail(user,to,msg.as_string()) | |
smtpServer.quit() | |
print "send success" | |
except Exception,e: | |
print str(e) | |
return False | |
if __name__ == "__main__": | |
user = ""#邮件地址 | |
password = ""#密码 | |
to = ""#收件人 | |
subject = u"测试" | |
text = u"这是一封测试邮件" | |
sendMail(user,password,to,subject,text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment