Python 實現發郵件

2021-08-01 08:46:39 字數 918 閱讀 6298

#!/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

# 第三方 smtp 服務

mail_host="smtp.163.com"  #設定伺服器

mail_user="[email protected]"    #使用者名稱

mail_pass="123456"   #口令 

sender = '[email protected]'

receivers = ['[email protected]']  # 接收郵件,可設定為你的qq郵箱或者其他郵箱

message = mimetext('1111...', 'plain', 'utf-8')

message['from'] = "admin"

message['to'] =  "[email protected]"

subject = '222'

message['subject'] = header(subject, 'utf-8')

smtpobj = smtplib.smtp() 

smtpobj.connect(mail_host, 25)    # 25 為 smtp 埠號

smtpobj.login(mail_user,mail_pass)  

smtpobj.sendmail(sender, receivers, message.as_string())

print "郵件傳送成功"

注意:message['from']和message['to'] 不能使用header處理!!!

python 實現發郵件

coding utf 8 from email.header import header from email.mime.text import mimetext from email.utils import parseaddr,formataddr import smtplib class po...

python實現發郵件

話不多說,直接上 import smtplib from email.mime.text import mimetext from email.header import header def sendmail sender 163.com 發件人賬號 password 發件人密碼 receiver...

python 發郵件 python發郵件

python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。這是乙個簡單的語法,用來建立乙個smtp物件,稍後將演示如何用它來傳送電子郵件 import smtplib smtpobj smtplib.smtp ...