歡迎您光臨本站 註冊首頁

python3.6使用SMTP協議發送郵件

←手機掃碼閱讀     sl_ivan @ 2020-06-10 , reply:0

本文實例為大家分享了python3.6使用SMTP協議發送郵件的具體代碼,供大家參考,具體內容如下

代碼如下:

  # !/usr/bin/python3  # coding: utf-8    import smtplib    from email.header import Header  from email.mime.text import MIMEText  from email.utils import parseaddr  from email.utils import formataddr      def format_addr(s):   name, addr = parseaddr(s)   return formataddr((Header(name, "utf-8").encode(), addr))      from_email = "email@qq.com" # 郵箱地址  from_email_pwd = "password" # 郵箱密碼  to_email = "to@qq.com" # 接收者郵箱  smtp_server = "smtp.exmail.qq.com" # 協議    msg = MIMEText("hellohello, send by python", "html", "utf-8")  msg["From"] = format_addr("%s" % (from_email))  msg["To"] = format_addr("%s" % (to_email))  msg["Subject"] = Header("python email", "utf-8").encode()    server = smtplib.SMTP_SSL(smtp_server, port=465) # 騰訊企業郵箱配置(SSL)  # server = smtplib.SMTP(smtp_server, port=25) # 網易126郵箱  server.set_debuglevel(1)  server.login(from_email, from_email_pwd)  server.sendmail(from_email, [to_email], msg.as_string())  server.quit()

 

網易郵箱配置:
 

開啟IMAP/SMTP服務,SMTP服務器: smtp.126.com,設置開啟客戶端授權密碼(代碼裡的登錄密碼需使用此授權密碼)

  smtp_server = "smtp.126.com" # 協議  server = smtplib.SMTP(smtp_server, port=25) # 網易126郵箱

 

騰訊企業郵箱配置:

開啟IMAP/SMTP服務
 發送服務器:smtp.exmail.qq.com(使用SSL,端口號465),密碼為登錄密碼。

  smtp_server = "smtp.exmail.qq.com" # 協議  server = smtplib.SMTP_SSL(smtp_server, port=465)

 

                                                       

   


[sl_ivan ] python3.6使用SMTP協議發送郵件已經有240次圍觀

http://coctec.com/docs/python/shhow-post-237810.html