前言:
刚学习python ,简单的代码学习下 就是实现发送邮件的代码。如下:
# -*- coding:utf-8 -*- # ********************************** # ** http://weibo.com/lixiaodaoaaa # # ****** by:lixiaodaoaaa *********** def sendEmail(mailAddress, mailText): import smtplib from email.mime.text import MIMEText from email.utils import formataddr msg = MIMEText(mailText, 'plain', 'utf-8') msg['From'] = formataddr(["Dog", 'lixiaodaoaaa@126.com']) msg['To'] = formataddr([mailText, mailAddress]) msg['Subject'] = "主题" try: server = smtplib.SMTP("smtp.126.com", 25) server.login("lixiaodaoaaa@126.com", "DogYouXiangPassword") server.sendmail('lixiaodaoaaa@126.com', [mailAddress], msg.as_string()) server.quit() return True except: return False userInputEmailAdd = input("请输入你要发送的邮件地址: ") userInputEmailContent = input("请输入你想说的话: ") if sendEmail(userInputEmailAdd, userInputEmailContent): print("发送成功") else: print("发送失败。。。。。")