您的位置:首页 > 编程语言 > Python开发

Python 3(13)使用 smtplib 模块发送 SMTP 邮件

2018-01-26 02:08 976 查看

Python3入门专栏

http://blog.csdn.net/column/details/19679.html




Python 发送 SMTP 邮件

python的smtplib模块对smtp协议进行了简单的封装,可以用于发送smtp协议的电子邮件;

发送普通邮件

 
importsmtplib
fromemail.headerimportHeader
fromemail.mime.textimportMIMEText
#使用163邮箱的SMTP服务
mail_host="smtp.163.com"       #smtp服务器
mail_user="helloworld_assad@163.com" #smtp服务器验证用户名
mail_password="*************"    #smtp服务器验证密码(smtp授权码)
sender="helloworld_assad@163.com"     #发送地址
receivers=["yulinying_1994@outlook.com"] #接收地址
#信息对象(dict对象)
message=MIMEText
159ac
("Helloworld","plain","utf-8")
message["From"]=Header("assad","utf-8")
message["To"]=Header("Mr.lin","utf-8")
message["Subject"]=Header("pythonsmtp邮件测试","utf-8")
try:
  server=smtplib.SMTP()         #创建SMTP对象
  server.connect(mail_host,25)      #连接smtp服务器,默认端口25
  server.login(mail_user,mail_password) #登陆smtp服务器
  server.sendmail(sender,receivers,message.as_string()) #发送邮件
  print("sendemailsuccess")
exceptsmtplib.SMTPExceptionaserr:
  print("sendemailfail")
  print("Errormessage:",err)

发送HTML文本邮件

 
importsmtplib
fromemail.headerimportHeader
fromemail.mime.textimportMIMEText
#使用163邮箱的SMTP服务
mail_host="smtp.163.com"       #smtp服务器
mail_user="helloworld_assad@163.com" #smtp服务器验证用户名
mail_password="*************"    #smtp服务器验证密码(smtp授权码)
sender="helloworld_assad@163.com"     #发送地址
receiver="yulinying_1994@outlook.com" #接收地址
mail_msg="""
<h1>HelloWorld!</h1>
<p>thisisatestemailfrompythonsmtp</p>
"""
#信息对象(dict对象)
message=MIMEText(mail_msg,"html","utf-8")
message["From"]=Header(sender,"utf-8")
message["To"]=Header(receiver,"utf-8")
message["Subject"]=Header("pythonsmtphtml邮件测试","utf-8")
try:
  server=smtplib.SMTP()         #创建SMTP对象
  server.connect(mail_host,25)      #连接smtp服务器,默认端口25
  server.login(mail_user,mail_password) #登陆smtp服务器
  server.sendmail(sender,receiver,message.as_string()) #发送邮件
  print("sendemailsuccess")
exceptsmtplib.SMTPExceptionaserr:
  print("sendemailfail")
  print("Errormessage:",err)

发送带有附件的邮件

 
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.mime.multipartimportMIMEMultipart
fromemail.headerimportHeader
#使用163邮箱的SMTP服务
mail_host="smtp.163.com"       
mail_user="helloworld_assad@163.com"
mail_password="*************"    
sender="helloworld_assad@163.com" 
receiver="yulinying_1994@outlook.com"
#邮件信息对象
message=MIMEMultipart()
message['From']=Header(sender,'utf-8')
message['To']= Header(receiver,'utf-8')
message['Subject']=Header('pythonsmtp邮件附件测试','utf-8')
#邮件正文内容
message.attach(MIMEText('thisisatestemailfrompythonsmtp','plain','utf-8'))
#附件1:当前目录下的test1.txt文件
att1=MIMEText(open('test1.txt','rb').read(),'base64','utf-8')
att1["Content-Type"]='application/octet-stream'
att1["Content-Disposition"]='attachment;filename="test1.txt"'
#附件2:当前目录下的test2.txt文件
att2=MIMEText(open('test2.txt','rb').read(),'base64','utf-8')
att2["Content-Type"]='application/octet-stream'
att2["Content-Disposition"]='attachment;filename="test2.txt"'
#向邮件对象添加附件对象
message.attach(att1)
message.attach(att2)
try:
  server=smtplib.SMTP()        
  server.connect(mail_host,25)      
  server.login(mail_user,mail_password) 
  server.sendmail(sender,receiver,message.as_string()) 
  print("sendemailsuccess")
exceptsmtplib.SMTPExceptionaserr:
  print("sendemailfail")
  print("Errormessage:",err)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: