#!/usr/bin/python
# Encoding=utf-8
# Filename:send_email.py
From Email.mime.image import Mimeimage
From Email.mime.multipart import Mimemultipart
From Email.mime.text import Mimetext
Import Smtplib
Class SendEmail:
# Constructors: Initializing basic information
def __init__ (self, host, user, passwd):
Linfo = User.split ("@")
Self._user = user
Self._account = linfo[0]
Self._me = Self._account + "<" + Self._user + ">"
Server = Smtplib. SMTP ()
Server.connect (host)
Server.login (Self._account, passwd)
Self._server = Server
# Send a file or HTML message
def sendtxtmail (self, to_list, sub, content, subtype= ' html '):
# If a text message is sent, the _subtype is set to plain
# If you send an HTML message, _subtype is set to HTML
msg = mimetext (content, _subtype=subtype, _charset= ' Utf-8 ')
msg[' Subject ' = Sub
Msg[' from '] = Self._me
Msg[' to '] = ";". Join (To_list)
Try
Self._server.sendmail (Self._me, To_list, msg.as_string ())
Return True
Except Exception, E:
Print str (e)
Return False
# Send files or HTML messages with attachments
def sendattachmail (self, to_list, sub, content, subtype= ' html '):
# Create an instance with an attachment
msg = Mimemultipart ()
# Add Annex 1
ATT1 = Mimetext (open (R ' D:\javawork\PyTest\src\main.py ', ' RB '). Read (), ' base64 ', ' Utf-8 '
att1["Content-type"] = ' application/octet-stream '
# The filename here can be arbitrarily written, what name to write, what name to display in the message
att1["content-disposition"] = ' attachment; Filename= "main.py"
Msg.attach (ATT1)
# Add Annex 2
ATT2 = Mimetext (open (R ' D:\javawork\PyTest\src\main.py ', ' RB '). Read (), ' base64 ', ' Utf-8 '
att2["Content-type"] = ' application/octet-stream '
att2["content-disposition"] = ' attachment; Filename= "Main.txt"
Msg.attach (ATT2)
# Add Mail Content
Msg.attach (Mimetext (content, _subtype=subtype, _charset= ' Utf-8 '))
msg[' Subject ' = Sub
Msg[' from '] = Self._me
Msg[' to '] = ";". Join (To_list)
Try
Self._server.sendmail (Self._me, To_list, msg.as_string ())
Return True
Except Exception, E:
Print str (e)
Return False
# Send files or HTML messages with attachments
def sendimagemail (self, to_list, sub, content, subtype= ' html '):
# Create an instance with an attachment
msg = Mimemultipart ()
# Add Mail Content
Msg.attach (Mimetext (content, _subtype=subtype, _charset= ' Utf-8 '))
# Add Picture Accessories
Image = Mimeimage (open (R ' D:\javawork\PyTest\src\test.jpg ', ' RB '). Read ())
#附件列表中显示的文件名
Image.add_header (' content-disposition ', ' attachment;filename=p.jpg ')
Msg.attach (image)
msg[' Subject ' = Sub
Msg[' from '] = Self._me
Msg[' to '] = ";". Join (To_list)
Try
Self._server.sendmail (Self._me, To_list, msg.as_string ())
Return True
Except Exception, E:
Print str (e)
Return False
# destructor: Releasing resources
def __del__ (self):
Self._server.quit ()
Self._server.close ()
Mailto_list = [' xxx@163.com ']
Mail = SendEmail (' smtp.163.com ', ' xxx@163.com ', ' xxxxxx ')
If Mail.sendtxtmail (mailto_list, "Test mail", "Hello world! <br><br>Print "Send Success"
Else
Print "Send Failed"
If Mail.sendattachmail (mailto_list, "test message-with two attachments", "Hello world! <br><br>Print "Send Success"
Else
Print "Send Failed"
If Mail.sendimagemail (mailto_list, "test message-with an attachment to a picture", "Hello world! <br><br>Print "Send Success"
Else
Print "Send Failed"