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
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msg=MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
msg['Subject']=Header(subject,'utf-8')
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
HTML形式的邮件
[python]view plain
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msg=MIMEText('<html><h1>你好</h1></html>','html','utf-8')
msg['Subject']=subject
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
带图片的HTML邮件
[python]view plain
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msgRoot=MIMEMultipart('related')
msgRoot['Subject']='testmessage'
msgText=MIMEText('<b>Some<i>HTML</i>text</b>andanimage.<br><imgsrc="cid:image1"><br>good!','html','utf-8')
msgRoot.attach(msgText)
fp=open('h:\python\1.jpg','rb')
msgImage=MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID','<image1>')
msgRoot.attach(msgImage)
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()
带附件的邮件
[python]view plain
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msgRoot=MIMEMultipart('related')
msgRoot['Subject']='testmessage'
#构造附件
att=MIMEText(open('h:\python\1.jpg','rb').read(),'base64','utf-8')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="1.jpg"'
msgRoot.attach(att)
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()
群邮件
[python]view plain
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
sender='***'
receiver=['***','****',……]
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msg=MIMEText('你好','text','utf-8')
msg['Subject']=subject
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
各种元素都包含的邮件
[python]view plain
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
#Createmessagecontainer-thecorrectMIMEtypeismultipart/alternative.
msg=MIMEMultipart('alternative')
msg['Subject']="Link"
#Createthebodyofthemessage(aplain-textandanHTMLversion).
text="Hi! Howareyou? Hereisthelinkyouwanted: http://www.python.org"
html="""
<html>
<head></head>
<body>
<p>Hi!<br>
Howareyou?<br>
Hereisthe<ahref="http://www.python.org">link</a>youwanted.
</p>
</body>
</html>
"""
#RecordtheMIMEtypesofbothparts-text/plainandtext/html.
part1=MIMEText(text,'plain')
part2=MIMEText(html,'html')
#.
#AccordingtoRFC2046,,inthiscase
#theHTMLmessage,isbestandpreferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att=MIMEText(open('h:\python\1.jpg','rb').read(),'base64','utf-8')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="1.jpg"'
msg.attach(att)
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
基于SSL的邮件
[python]view plain
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msg=MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
msg['Subject']=Header(subject,'utf-8')
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
文件形式的邮件
[python]view plain
HTML形式的邮件
[python]view plain
带图片的HTML邮件
[python]view plain
带附件的邮件
[python]view plain
群邮件
[python]view plain
各种元素都包含的邮件
[python]view plain
基于SSL的邮件
[python]view plain
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msg=MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
msg['Subject']=Header(subject,'utf-8')
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
#Createmessagecontainer-thecorrectMIMEtypeismultipart/alternative.
msg=MIMEMultipart('alternative')
msg['Subject']="Link"
#Createthebodyofthemessage(aplain-textandanHTMLversion).
text="Hi! Howareyou? Hereisthelinkyouwanted: http://www.python.org"
html="""
<html>
<head></head>
<body>
<p>Hi!<br>
Howareyou?<br>
Hereisthe<ahref="http://www.python.org">link</a>youwanted.
</p>
</body>
</html>
"""
#RecordtheMIMEtypesofbothparts-text/plainandtext/html.
part1=MIMEText(text,'plain')
part2=MIMEText(html,'html')
#.
#AccordingtoRFC2046,,inthiscase
#theHTMLmessage,isbestandpreferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att=MIMEText(open('h:\python\1.jpg','rb').read(),'base64','utf-8')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="1.jpg"'
msg.attach(att)
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.textimportMIMEText
sender='***'
receiver=['***','****',……]
subject='pythonemailtest'
smtpserver='smtp.163.com'
username='***'
password='***'
msg=MIMEText('你好','text','utf-8')
msg['Subject']=subject
smtp=smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
#!/usr/bin/envpython3
#coding:utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.imageimportMIMEImage
sender='***'
receiver='***'
subject='pythonemailtest'
smtpserver='smtp.163.com'
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!"
如果要不经过邮件系统直接发,通常会被当作垃圾邮件扔了,所以还是这样吧。