Example 1
The code is as follows |
Copy Code |
From Email.mime.text import Mimetext From Email.mime.audio import Mimeaudio From Email.mime.image import Mimeimage From Email.mime.multipart import Mimemultipart #发送邮件 Def sendMail (): Sender = "haiwil2012@yahoo.cn" Receiver = "haiwil2013@yahoo.cn" Subject = "Testing" BODY = "----test send a message----n----Congratulations on your successful sending a message----n----Test send a message----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 ' |
Supports SMTP authentication. The code is as follows:
The code is as follows |
Copy Code |
#!/usr/bin/env python #-*-CODING:GBK-*- #导入smtplib和MIMEText Import Smtplib From Email.mime.text import Mimetext ############# #要发给谁, 2 people here. mailto_list=["aaa@juyimeng.com", "bbb@juyimeng.com"] ##################### #设置服务器, username, password, and suffix of the mailbox Mail_host= "Smtp.126.com" mail_user= "XXX" Mail_pass= "YYY" mail_postfix= "126.com" ###################### def send_mail (to_list,sub,content): ''' To_list: Who is it sent to? Sub: Theme Content: Contents Send_mail ("aaa@126.com", "sub", "Content") ''' Me=mail_user+ "<" +mail_user+ "@" +mail_postfix+ ">" msg = Mimetext (content) msg[' Subject ' = Sub msg[' from ' = Me Msg[' to '] = ";". Join (To_list) Try s = smtplib. SMTP () S.connect (Mail_host) S.login (Mail_user,mail_pass) S.sendmail (Me, To_list, msg.as_string ()) S.close () Return True Except Exception, E: Print str (e) Return False if __name__ = = ' __main__ ': If Send_mail (mailto_list, "subject", "Content"): Print "Send Success" Else Print "Send Failed" |