import smtplib from email.mime.text import MIMEText mail_host = 'smtp.163.com' mail_user = '發件人郵箱' mail_pwd = '授權碼' sender = '發件人郵箱' receivers = ['收件人郵箱'] content = 'python使用163smtp發送郵件' title = '人生苦短' def sendmail(): message = MIMEText(content, 'plain', 'utf-8') # 內容,格式,編碼 message['From'] = "{}".format(sender) message['To'] = ','.join(receivers) message['Subject'] = title try: smtpobj = smtplib.SMTP_SSL(mail_host, 465) # 啟用SSL發信,端口一般是465 smtpobj.login(mail_user, mail_pwd) # 登陸驗證 smtpobj.sendmail(sender, receivers, message.as_string()) print('mail has been send successfully') except Exception as e: print(e) sendmail()