Script
#!/usr/bin/python#-*-coding:utf-8-*-#Python Mail for chenglee#if fileformat=dos, update fileformat=unix#code:set Fileformat=unix#check:set ff? import smtplibfrom email.mime.text import mimetextfrom email.utils import Formataddrmy_ Sender= ' [email protected] ' # sender email account my_pass = ' xxxxxxxxxxx ' # Sender's mailbox password (at that time request SMTP password) my_user= ' [EMAIL&NB Sp;protected] ' # recipient email account, my side sent to myself Def Mail (): Ret=true try:msg=mimetext (' 11 fill in the message content ', ' plain ', ' utf-8 ') msg[' from ']=formataddr (["Sender nickname", My_sender]) # corresponding sender's mailbox nickname in parentheses, Sender's mailbox account msg[' to ']=formataddr (["Recipient nickname", My_user]) # The corresponding recipient's mailbox nickname in parentheses, the recipient's email account msg[' Subject ']= "11 mail subject-Test" # The subject of the message, can also be said to be the title SERVER=SMTPLIB.S Mtp_ssl ("smtp.qq.com", 465) # The SMTP server in the sender's mailbox, the port is 465 server.login (My_sender, my_pass) # brackets correspond to the sender's mailbox account, email password Server.sendmail (My_sender,[my_user,],msg.as_string ()) # in parentheses corresponds to the sender's mailbox account, recipient's mailbox account, outgoing mail server.quit () # Close connection except exception:# if the statement in the try is not executed, the following Ret=false Ret=false return Retret=mail () if Ret:print ("message sent successfully") Else:print ("message sent Failed")
When used under Windows, the first two lines can be left!
Test Results
Windows
Linux
Tips
As a sender, two services are required.
Python Mail Send script (linux,windows) generic