導航:首頁 > 編程語言 > python發郵件

python發郵件

發布時間:2022-01-15 21:46:57

A. 如何使用python發郵件

直接貼點代碼,感受下

#!/usr/bin/python

#-*-coding:UTF-8-*-importsmtplib

fromemail.mime.textimportMIMEText

fromemail.headerimportHeadersender='XXXXX'
receivers=['[email protected]']#接收郵件,可設置為你的QQ郵箱或者其他郵箱#三個參數:第一個為文本內容,第二個plain設置文本格式,第三個utf-8設置編碼

message=MIMEText('Python郵件發送測試...','plain','utf-8')

message['From']=Header("測試",'utf-8')

message['To']=Header("測試",'utf-8')subject='PythonSMTP郵件測試'

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

try:

smtpObj=smtplib.SMTP('localhost')

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

print"郵件發送成功"

exceptsmtplib.SMTPException:

print"Error:無法發送郵件"

B. 如何使用Python發送帶的郵件

.headerimportHeaderfromemail.mime.textimportMIMETextfromemail.utilsimportparseaddr,formataddrdefsend_email(from_addr,to_addr,subject,password):
msg=MIMEText("郵件正文",'html','utf-8')
msg['From']=u'<%s>'%from_addr
msg['To']=u'<%s>'%to_addr
msg['Subject']=subject

smtp=smtplib.SMTP_SSL('smtp.163.com',465)
smtp.set_debuglevel(1)
smtp.ehlo("smtp.163.com")
smtp.login(from_addr,password)
smtp.sendmail(from_addr,[to_addr],msg.as_string())if__name__=="__main__":
#這里的密碼是開啟smtp服務時輸入的客戶端登錄授權碼,並不是郵箱密碼
#現在很多郵箱都需要先開啟smtp才能這樣發送郵件
send_email(u"from_addr",u"to_addr",u"主題",u"password")

C. 我用python發郵件。出現以下問題,求解答

1、准備兩個郵箱帳號,一個是常用的(接收端),另一個可以注冊網易163郵箱或者foxmail郵箱也可(發送端),本次我使用兩個QQ郵箱進行演示。

2、在郵箱的設置

3、賬戶中開啟SMTP功能,如下圖:

8、如果能成功收到郵件的話就說明完成了。

D. 如何用python自動發送郵件

注意如果使用qq的smtp轉發功能的話,需要在設置頁面中將該功能打開,然後設定密碼,改密碼就是賦值給下文password中的。

開啟smtp轉發功能文檔


[python]view plain

#fromemail.MIMETextimportMIMEText#

fromemail.MIMETextimportMIMEText

fromemail.HeaderimportHeader

msg=MIMEText('hello,sendbypython','plain','utf-8');

from_addr="*****@qq.com"#

password="******"#password

to_addr="****@qq.com"#targetemailaddress

smtp_server="smtp.qq.com"#smtpseverdomainforqqissmtp.qq.com

importsmtplib

server=smtplib.SMTP(smtp_server,25);

server.set_debuglevel(1)

server.login(from_addr,password);

server.sendmail(from_addr,[to_addr],msg.as_string())

server.quit()



上面這個是沒有主題的,有主題的話建議採用下面這個代碼

下面是連續發送有主題的文件10封


[python]view plain

fromemail.HeaderimportHeader

fromemail.MIMETextimportMIMEText

fromemailimportencoders

fromemail.utilsimportparseaddr,formataddr

importsmtplib

def_format_addr(s):

name,addr=parseaddr(s)

returnformataddr((Header(name,'utf-8').encode(),addr))

from_addr="*****@qq.com"#

password="******"#password

to_addr="****@qq.com"#targetemailaddress

smtp_server="smtp.qq.com"#smtpseverdomainforqqissmtp.qq.com

fornuminrange(1,11):

msg=MIMEText('hello,sendbyTom','plain','utf-8');

msg['From']=_format_addr('Tom<%s>'%from_addr)

msg['To']=_format_addr('addministrator<%s>'%to_addr)

msg['Subject']=Header('ThegreetingfromTom','utf-8').encode()

server=smtplib.SMTP(smtp_server,25)

server.set_debuglevel(1)

server.login(from_addr,password)

#server.sendmail(from_addr,[to_addr],msg.as_string())

server.sendmail(from_addr,[to_addr],msg.as_string())

server.quit()

E. 如何在python程序中發郵件

比如我用自己的 139 郵箱 [email protected]
發送郵件到網易郵箱 也是我的辦公郵箱 [email protected]

以下代碼調試通過:

#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader

sender='[email protected]'
receiver='[email protected]'
subject='pythonemailtest'
smtpserver='smtp.139.com'
username='[email protected]'
password='xxxxxxxx'

msg=MIMEText('你好lucia這是你的第一封python發出的郵件','text','utf-8')
#中文需參數『utf-8',單位元組字元不需要
msg['Subject']=Header(subject,'utf-8')

smtp=smtplib.SMTP()
smtp.connect('smtp.139.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()

運行結果:

F. 如何用Python發郵件

文件形式的郵件

[python]view plain

G. python 怎麼發email

1、登錄郵件服務
復制代碼代碼如下:

#!/usr/bin/envpython
#-*-coding:utf-8-*-
#python2.7x
#send_simple_email_by_account.py@2014-07-30
#author:orangleliu

'''''
使用python寫郵件simple
使用126的郵箱服務
'''

importsmtplib
fromemail.mime.textimportMIMEText

SMTPserver='smtp.126.com'
sender='[email protected]'
password="xxxx"

message='IsendamessagebyPython.你好'
msg=MIMEText(message)

msg['Subject']='TestEmailbyPython'
msg['From']=sender
msg['To']=destination

mailserver=smtplib.SMTP(SMTPserver,25)
mailserver.login(sender,password)
mailserver.sendmail(sender,[sender],msg.as_string())
mailserver.quit()
print'sendemailsuccess'
2、調用sendmail命令linux
復制代碼代碼如下:

#-*-coding:utf-8-*-
#python2.7x
#send_email_by_.py
#author:orangleliu
#date:2014-08-15
'''''
用的是sendmail命令的方式

這個時候郵件還不定可以發出來,hostname配置可能需要更改
'''

fromemail.mime.textimportMIMEText
fromsubprocessimportPopen,PIPE

defget_sh_res():
p=Popen(['/Application/2.0/nirvana/logs/log.sh'],stdout=PIPE)
returnstr(p.communicate()[0])

defmail_send(sender,recevier):
print"getemailinfo..."
msg=MIMEText(get_sh_res())
msg["From"]=sender
msg["To"]=recevier
msg["Subject"]="Yestodayinterfacelogresults"
p=Popen(["/usr/sbin/sendmail","-t"],stdin=PIPE)
res=p.communicate(msg.as_string())
print'mailsended...'

if__name__=="__main__":
s="[email protected]"
r="[email protected]"
mail_send(s,r)

3、使用smtp服務來發送(本地或者是遠程伺服器)
復制代碼代碼如下:

#!/usr/bin/envpython
#-*-coding:utf-8-*-
#python2.7x
#send_email_by_smtp.py
#author:orangleliu
#date:2014-08-15
'''''
linux下使用本地的smtp服務來發送郵件
前提要開啟smtp服務,檢查的方法
#ps-ef|grepsendmail
#telnetlocalhost25

這個時候郵件還不定可以發出來,hostname配置可能需要更改
'''
importsmtplib
fromemail.mime.textimportMIMEText
fromsubprocessimportPopen,PIPE


defget_sh_res():
p=Popen(['/Application/2.0/nirvana/logs/log.sh'],stdout=PIPE)
returnstr(p.communicate()[0])

defmail_send(sender,recevier):
msg=MIMEText(get_sh_res())
msg["From"]=sender
msg["To"]=recevier
msg["Subject"]="Yestodayinterfacelogresults"
s=smtplib.SMTP('localhost')
s.sendmail(sender,[recevier],msg.as_string())
s.quit()
print'sendmailfinished...'

if__name__=="__main__":
s="[email protected]"
r=s
mail_send(s,r)

H. 如何用python發送email

有好幾個模塊,可以實現.

這里用smtplib和email來實現了一個簡單的不帶附件的郵件發送

以163郵件為服務郵箱配置的。

#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.mime.multipartimportMIMEMultipart
importsys
reload(sys)
sys.setdefaultencoding("utf8")
#配置收發件人,可以發送給多個
recvaddress=['[email protected]','[email protected]']
#163的用戶名和密碼,修改為你自己的
sendaddr_name='[email protected]'
sendaddr_pswd='your_password'


classSendMail:
def__init__(self,recver=None):
"""接收郵件的人:listortuple"""
ifrecverisNone:
self.sendTo=recvaddress
else:
self.sendTo=recver

defsend(self,sub,content):
"""發送郵件"""
self.msg=MIMEMultipart()
#設置發送郵件
self.msg['from']=sendaddr_name
try:
#設置郵箱伺服器
smtp=smtplib.SMTP('smtp.163.com',25)
#登錄
smtp.login(sendaddr_name,sendaddr_pswd)
#設置發送主題
self.msg['Subject']=sub
#設置郵件內容
self.msg.attach(MIMEText(content))
#發送郵件
smtp.sendmail(self.msg['from'],self.sendTo,self.msg.as_string())
smtp.close()
print(u"發送郵件成功")
exceptException,e:
print(u'發送郵件失敗:'+str(e))
raise

#調用
s=SendMail()
subject="主題"
content="這里是郵件內容"
s.send(subject,content)

I. 如何通過python發送郵件啊

一般最好有個smtp伺服器,比如說你在163注冊個郵箱,這樣可以用smtplib通過這個郵箱來發送。以下是示例:

#-*- coding:utf8 -*-
import smtplib
import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.mime.text import MIMEText

mail_host="smtp.163.com"
mail_user="yourusername"
mail_pass="yourpassword"
mail_postfix="mail.163.com"

def sendmail(to_list,sub,con):
"""發送郵件
"""
# translation
me = mail_user+"<"+mail_user+"@"+mail_postfix+">"

msg = MIMEMultipart('related')
msg['Subject'] = email.Header.Header(sub,'utf-8')
msg['From'] = me
msg['To'] = ";".join(to_list)
msg.preamble = 'This is a multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msgText = MIMEText(con, 'plain', 'utf-8')
msgAlternative.attach(msgText)
msg.attach(msgAlternative)

try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.quit()

except Exception,e:
return False

return True

if __name__ == '__main__':
if sendmail(['[email protected]'],"測試","測試"):
print "Success!"
else:
print "Fail!"

如果要不經過郵件系統直接發,通常會被當作垃圾郵件扔了,所以還是這樣吧。

閱讀全文

與python發郵件相關的資料

熱點內容
編程拖放 瀏覽:40
linux卸載tomcat 瀏覽:875
手機時間如何校正到伺服器 瀏覽:81
創造與魔法瞬移源碼百度 瀏覽:882
反射優化java 瀏覽:874
硬體加密播放盒子 瀏覽:923
xp點擊文件夾選項沒反應 瀏覽:537
蘋果不顯示桌面的app怎麼刪除 瀏覽:864
安卓手機怎麼換國際服 瀏覽:415
神獸領域安卓怎麼下載 瀏覽:250
單片機交通燈ad原理圖 瀏覽:413
多功能解壓磁鐵筆 瀏覽:80
少兒編程火箭升空 瀏覽:401
蘭斯10游戲解壓碼 瀏覽:42
手機proxy伺服器地址 瀏覽:449
吉他清音壓縮 瀏覽:301
簡歷模板程序員 瀏覽:882
螺桿壓縮機虛標型號 瀏覽:953
idea開發項目伺服器ip地址 瀏覽:125
串口伺服器出現亂碼怎麼解決 瀏覽:950