Two python smtp outgoing mail code

Source: Internet
Author: User
Tags mail code

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.