案例一python
#!/usr/bin/env python3.6 # -*- coding=utf-8 -*- import smtplib from email.mime.text import MIMEText _user = "648613081@qq.com" #發送帳戶 _pwd = "這裏改爲你的受權碼" #發送帳戶的受權碼 _to = "648613081@qq.com" #接收帳戶 msg = MIMEText("this is a email from python,ha ha ha ...") #郵件內容 msg["Subject"] = "這裏是主題" #郵件主題 msg["From"] = _user msg["To"] = _to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) #郵件服務器,端口 s.login(_user, _pwd) s.sendmail(_user, _to, msg.as_string()) s.quit() print ("發送成功") except (s.smtplib.SMTPException, e): print ("發送失敗")
案例二服務器
#!/usr/bin/env python #-*- coding: UTF-8 -*- import os,sys reload(sys) sys.setdefaultencoding('utf8') import getopt import smtplib from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from subprocess import * def sendqqmail(username,password,mailfrom,mailto,subject,content): #郵箱的smtp填寫在這裏 gserver = 'smtp.qq.com' #qq郵箱的端口號爲465 gport = 25 try: msg = MIMEText(unicode(content).encode('utf-8')) msg['from'] = mailfrom msg['to'] = mailto msg['Reply-To'] = mailfrom msg['Subject'] = subject #ssl鏈接,把下面改成smtp = smtplib.SMTP_SSL(gserver, gport) smtp = smtplib.SMTP(gserver, gport) smtp.set_debuglevel(0) smtp.ehlo() smtp.login(username,password) smtp.sendmail(mailfrom, mailto, msg.as_string()) smtp.close() except Exception,err: print "Send mail failed. Error: %s" % err def main(): to=sys.argv[1] subject=sys.argv[2] content=sys.argv[3] ##定義郵箱的帳號和密碼,你須要修改爲你本身的帳號和密碼(請不要把真實的用戶名和密碼放到網上公開,不然你會死的很慘) sendqqmail('1234567@qq.com','aaaaaaaaaa','1234567@qq.com',to,subject,content) if __name__ == "__main__": main() #####腳本使用說明###### #1. 首先定義好腳本中的郵箱帳號和密碼 #2. 腳本執行命令爲:python mail.py 目標郵箱 "郵件主題" "郵件內容" #3. 更改其餘郵箱後綴,只須要更改gserver = 'smtp.139.com',把郵箱的smtp開啓就能夠了 #4. gport = 25 爲端口,qq郵箱的端口爲465 #5. 若有ssl鏈接,代碼部分爲smtp = smtplib.SMTP_SSL(gserver, gport),在原基礎加_SSL