python3实现简单的邮件自动发送

import smtplib  
from email.mime.text import MIMEText  
mailto_list=['[email protected]'] 
mail_host="smtp.gmail.com"  #设置服务器
mail_user="XXXXX"    #用户名
mail_pass="XXXXX"   #口令 
mail_postfix="gmail.com"  #发件箱的后缀
  
def send_mail(to_list,sub,content):  
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"  
    msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
    msg['Subject'] = sub  
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        server = smtplib.SMTP()  
        server.connect(mail_host)  
        server.login(mail_user,mail_pass)  
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        return True  
    except Exception as e:  
        print(str(e))  
        return False

def getMessage(pathOfMessageTXT):
	messageFile = open(pathOfMessageTXT,'w')
	titleOfTheMessage = messageFile.readline()
	contentOfTheMessage = messageFile.read(77777)
	return (titleOfTheMessage,contentOfTheMessage)

if __name__ == '__main__':
    (titleOfTheMessage,contentOfTheMessage) = getMessage('Message.txt')   
    if send_mail(mailto_list,titleOfTheMessage,contentOfTheMessage):  
        print("发送成功")  
    else:  
        print("发送失败")  


你可能感兴趣的:(Python)