Python SMTPLIB

来源:互联网 发布:藏文输入法软件下载 编辑:程序博客网 时间:2024/06/14 21:41

Python简单邮件代码


#! /user/bin/python
#-*-coding: UTF-8-*-


import smtplib, mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart




msg = MIMEMultipart()
msg["From"] = "xxxxxx"
msg["To"] = "xxxxxx"
msg["Subject"] = "Test SMTP by Python"


text = MIMEText("This is text content")
msg.attach(text)


filename = r'F:\ttt.txt'
ctype,encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype,subtype = ctype.split('/',1)


att = MIMEImage(open(filename,'r').read(),subtype)
print ctype,encoding
att["Content-Disposition"] = "attachment;filename='ttt.txt'"
msg.attach(att)


smtp = smtplib.SMTP()
smtp.connect("smtp.163.com")
smtp.login("xxxx","xxxx")
smtp.sendmail(msg["From"],msg["To"],msg.as_string())


smtp.quit()
print "send mail successfully"

0 0
原创粉丝点击