Import Smtplib # e-mail
From Email.mime.text import Mimetext # Constructs the message content, object
Username = ' [email protected] '
Password = ' jiqiren123 '
msg = Mimetext (' Hello, fast New Year, happy! ') # message Content
msg[' Subject ' = ' Happy New Year ' # title
msg[' from ' = ' [email protected] ' # who sent the
Msg[' to '] = ' [email protected] ' # sent to WHO
msg[' cc '] = ' [email protected] ' # cc to WHO
SMTP = Smtplib. SMTP (' smtp.163.com ', port=25) # and Mailbox server establish connection
# SMTP = Smtplib. Smtp_ssl (' smtp.11.com ', port=465) # Outbox is QQ mailbox with this
Smtp.login (username, password) # login Email
Smtp.sendmail (username, ' [email protected] ', msg.as_string ()) # who sent it to?
Smtp.quit ()
Print (' Send success! ')
Import Smtplib
From Email.mime.text import Mimetext
From Email.mime.multipart import Mimemultipart
Username = ' [email protected] '
Email_host = ' smtp.163.com '
passwd = ' jiqiren123 '
recv = ' [email protected],[email protected] '
title = ' message header '
Content = ' Send mail test '
msg = Mimemultipart ()
File = ' URL encoding. py ' # attachment filename
ATT = mimetext (open (file, encoding= ' Utf-8 '). Read ()) # An object that constructs an attachment
att["Content-type"] = ' application/octet-stream '
att["content-disposition"] = ' attachment; Filename= "%s" '% file # This is the name of the attachment displayed in the message
Msg.attach (ATT) # Add the attachment you just created to the body
Msg.attach (Mimetext (' e-mail content .... ') # Contents of the message body
msg[' Subject ' = title # message subject
msg[' from ' = Username # sender Account
msg[' to ' = recv # recipient Account List
# SMTP = Smtplib. Smtp_ssl (email_host,port=456) #qq邮箱
SMTP = Smtplib. SMTP (Email_host, port=25) # Other mailboxes
Smtp.login (username, passwd)
Smtp.sendmail (username, recv, msg.as_string ())
Smtp.quit ()
--------------------------------------------
Import Smtplib
Import OS
From Email.mime.text import Mimetext
From Email.mime.multipart import Mimemultipart
Import Base64
# QQ Authorization Code
# LFUHIZIUPLYKBGDD
# Vavqwyhdvjdzcabh
Class SendMail (object):
def __init__ (self, username, passwd, recv, title, content,
File=none, Ssl=false,
Email_host= ' smtp.qq.com ', port=25, ssl_port=465):
#:p Aram Username: User Name
#:p Aram passwd: Password
#:p Aram recv: Recipient, multiple to send list [' [email protected] ', ' [email protected]
#:p Aram Title: Message title
#:p Aram content: Message body
#:p Aram File: Attachment path, if not in current directory, to write absolute path, default no attachment
#:P Aram SSL: Secure link, default is normal
#:p Aram EMAIL_HOST:SMTP server address, default is 163 server
#:p Aram Port: Non-Secure link port, default is 25
#:p Aram Ssl_port: Secure link port, default is 465
Self.username = Username # user Name
self.passwd = passwd # password
SELF.RECV = recv # recipient, multiple to send list [' [email protected] ', ' [email protected]
Self.title = title # message Header
self.content = content # message body
self.file = File # attachment path, if not in current directory, to write absolute path
Self.email_host = email_host # SMTP server address
Self.port = port # normal ports
SELF.SSL = SSL # is secure link
Self.ssl_port = ssl_port # Secure link port
# SELF.SMTP = SMTP
def send_mail (self):
msg = Mimemultipart ()
# objects that send content
If Self.file: # Handles Attachments
file_name = Os.path.split (Self.file) [-1] # takes only the file name, does not fetch the path
Try
f = open (Self.file, ' RB '). Read ()
Except Exception as E:
Raise Exception (' Accessory can't open! ', E)
Else
ATT = Mimetext (f, "base64", "Utf-8")
att["Content-type"] = ' application/octet-stream '
# Base64.b64encode (File_name.encode ()). Decode ()
New_file_name = ' =?utf-8?b ' + base64.b64encode (File_name.encode ()). Decode () + '? = '
# here is the processing file named Chinese name, must be written so
att["content-disposition"] = ' attachment; Filename= "%s" '% New_file_name
Msg.attach (ATT)
Msg.attach (Mimetext (self.content)) # Contents of the message body
msg[' Subject ' = self.title # message subject
msg[' from ' = self.username # sender Account
Msg[' to '] = ', '. Join (SELF.RECV) # Recipient Account List
If Self.ssl:
SELF.SMTP = Smtplib. Smtp_ssl (Self.email_host, Port=self.ssl_port)
Else
SELF.SMTP = Smtplib. SMTP (Self.email_host, Port=self.port)
# objects that send mail servers
Self.smtp.login (Self.username, SELF.PASSWD)
Try
Self.smtp.sendmail (Self.username, Self.recv, msg.as_string ())
Pass
Except Exception as E:
Print (' Something went wrong. ', E)
Else
Print (' Send successfully! ‘)
Self.smtp.quit ()
if __name__ = = ' __main__ ':
m = SendMail (
Username= ' [email protected] ',
Passwd= ' Lfuhiziuplykbgdd ',
recv=[' [email protected] ', ' [email protected] ',
Title= ' Good New Year ',
Content= ' Happy New Year ',
File=r ' C:\Users\Wang\Desktop\ Happy New Year txt ',
Ssl=true,
)
M.send_mail ()
python--sending mail