python smtp发送邮件

来源:互联网 发布:开天斩数据 编辑:程序博客网 时间:2024/04/27 10:54
from email.mime.text import MIMEText  from email.mime.audio import MIMEAudiofrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipart#发送邮件def sendMail():    sender = "haiwil2012@yahoo.cn"    receiver = "haiwil2013@yahoo.cn"    subject = "Testing"    body = "----测试发送邮件----\n ----恭喜你成功发送了一封邮件----\n ----测试发送邮件----\n"    im = "1.jpg"    audio = "1.mp3"        m = MIMEMultipart()    m["to"] = receiver    m["from"] = sender    m["subject"] = subject        m.attach(MIMEText(body))    image = MIMEImage(open(im,"rb").read(),"jpg")    m.attach(image)    apart = MIMEAudio(open(audio,"rb").read(),"mp3")    apart.add_header("Content-Disposition","attachment",filename=audio)    m.attach(apart)        s = smtplib.SMTP()    s.connect("smtp.mail.yahoo.com")    s.login('haiwil2012@yahoo.cn', 'hf5555')    s.sendmail(sender,[receiver],m.as_string())    s.close    print 'send mail successfully'


原创粉丝点击