Copy codeThe Code is as follows:
#! /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:
# Constructor: Initialize 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
# Sending a file or html email
Def sendTxtMail (self, to_list, sub, content, subtype = 'html '):
# If a text message is sent, SET _ subtype to plain
# If an html message is sent, SET _ subtype to html.
Msg = MIMEText (content, _ subtype = subtype, _ charset = 'utf-8 ')
Msg ['subobject'] = sub
Msg ['from'] = self. _ me
Msg ['to'] = ";". join (to_list)
Try:
Self. _ server. sendmail (self. _ me, to_list, msg. as_string ())
Return True
Except t Exception, e:
Print str (e)
Return False
# Send an attachment file or html email
Def sendAttachMail (self, to_list, sub, content, subtype = 'html '):
# Create an instance with an attachment
Msg = MIMEMultipart ()
# Add Attachment 1
Att1 = MIMEText (open (r 'd: \ javawork \ PyTest \ src \ main. py', 'rb '). read (), 'base64', 'utf-8 ')
Att1 ["Content-Type"] = 'application/octet-stream'
# Here, the filename can be written at will. What name can be written, and what name is displayed in the email?
Att1 ["Content-Disposition"] = 'attachment; filename = "main. py "'
Msg. attach (att1)
# Add Attachment 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 email content
Msg. attach (MIMEText (content, _ subtype = subtype, _ charset = 'utf-8 '))
Msg ['subobject'] = sub
Msg ['from'] = self. _ me
Msg ['to'] = ";". join (to_list)
Try:
Self. _ server. sendmail (self. _ me, to_list, msg. as_string ())
Return True
Except t Exception, e:
Print str (e)
Return False
# Send an attachment file or html email
Def sendImageMail (self, to_list, sub, content, subtype = 'html '):
# Create an instance with an attachment
Msg = MIMEMultipart ()
# Add email content
Msg. attach (MIMEText (content, _ subtype = subtype, _ charset = 'utf-8 '))
# Adding image attachments
Image = MIMEImage (open (r 'd: \ javawork \ PyTest \ src \ test.jpg ', 'rb'). read ())
# File name displayed in the Attachment List
Image. add_header ('content-disposition', 'attachment?filename=p.jpg ')
Msg. attach (image)
Msg ['subobject'] = sub
Msg ['from'] = self. _ me
Msg ['to'] = ";". join (to_list)
Try:
Self. _ server. sendmail (self. _ me, to_list, msg. as_string ())
Return True
Except t Exception, e:
Print str (e)
Return False
# Destructor: release 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> Print "sent successfully"
Else:
Print "failed to send"
If mail. sendAttachMail (mailto_list, "test mail-with two attachments", "hello world! <Br> Print "sent successfully"
Else:
Print "failed to send"
If mail. sendImageMail (mailto_list, "test email-attachment with an image", "hello world! <Br> Print "sent successfully"
Else:
Print "failed to send"