【奥门威尼斯网址】python发送邮件实例源码

by admin on 2019年9月5日

# 导入 smtplib 和 MIMEText 

python发送邮件实例共享,python发送邮件实例

为了更加好的通晓邮件发送效用的落到实处,要先领悟邮件发送系统的差十分的少流程。首先 
电子邮件之间的竞相发送接受就疑似 
邮局邮件发送同样,从叁个站点(邮件发送服务器)到目标地站点(邮件接收服务器),然后指标地站点管理收到的邮件,并发送给接受人。每种邮件服务器即出任发送也担纲接受邮件,况兼种种服务器地址,端口号,配置等也比不上。

实在发送邮件的流水线就 两部:

第一:写邮件

写好发送方,接受方
写好宗旨
写好正文(富含附属类小部件,图片等)
把信件整理在联合具名

第二:发送邮件

接二连三发送邮件服务器
报到邮箱
发送邮件
退出邮箱

代码部分:

# -*- coding:utf-8 -*-
import smtplib
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#创建邮件对象,相当于信纸
msg = MIMEMultipart()
#邮件发送方
msg['From'] = 'XXXXXXXX.qq.com'
#邮件接收方
msg['To'] = '[email protected]'
#邮件主题
msg['Subject'] = Header('这是邮箱主题部分', charset='UTF-8')#中文主题

#文本内容
txt = '亲爱的用户'
part1 = MIMEText(txt, 'plain', 'utf-8')
#添加到邮件中
msg.attach(part1)

#超文本内容,放有图片,注意src路径,此为放在同一目录中
html = '<div><img src="cid:image1">亲爱的</div>'
#一定要设置邮件格式,否则可能会乱码,当时接手别人手中没加邮件格式的项目,坑了我半天
part2 = MIMEText(html, 'html', 'utf-8')
#添加到邮件中
msg.attach(part2)

#图片放到邮件中
# 指定图片为当前目录
fp = open('test.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)

#加入附件
att2 = MIMEText('这是附件里的文字', 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
#设置附件名为runoob.txt
att2["Content-Disposition"] = 'attachment; filename="runoob.txt"'
#附件放在邮件中
msg.attach(att2)

#非ssl方式的smtp(简单邮件发送协议),下面注释为ssl方式的smtp
smtp = smtplib.SMTP()
# smtp = smtplib.SMTP_SSL()

try:
  #连接主机地址
  smtp.connect('mail.yto.net.cn:25')
  #登录邮箱,需要邮箱账号,密码
  smtp.login('XXXXXXXX.qq.com','234123')
  #发送邮件,需要登录的邮箱号,发送方的邮箱号,发送内容
  smtp.sendmail('XXXXXXXX.qq.com', '[email protected]', msg.as_string())
  #退出邮箱
  smtp.quit()
  print 'The mail is sended successly.'
except smtplib.SMTPException,e:
  print "Error: 无法发送邮件",e

出殡结果:

奥门威尼斯网址 1

注意点:当本人邮箱作为邮件发送方时,注意邮件服务器的安装里把smtp服务打开

奥门威尼斯网址 2

以上就是本文的全体内容,希望对我们的学习抱有扶助,也愿意我们多多帮忙帮客之家。

为了更加好的敞亮邮件发送作用的贯彻,要先精通邮件发送系统的大约流程。首先
电子邮件之间…

  留言板回复笔者邮件提示

详解python 发送邮件实例代码,python发送邮件

python 发送邮件实例

文件方式的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
from emailheader import Header 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 

msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 

smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

HTML格局的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 

msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 

msg['Subject'] = subject 

smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

带图片的HTML邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 

msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image<br><img src="cid:image1"><br>good!','html','utf-8') 
msgRootattach(msgText) 

fp = open('h:\\python\\jpg', 'rb') 
msgImage = MIMEImage(fpread()) 
fpclose() 

msgImageadd_header('Content-ID', '<image1>') 
msgRootattach(msgImage) 

smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgRootas_string()) 
smtpquit() 

带附属类小部件的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 

msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 

#构造附件 
att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="jpg"' 
msgRootattach(att) 

smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgRootas_string()) 
smtpquit() 

群邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 

sender = '***' 
receiver = ['***','****',……] 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 

msg = MIMEText('你好','text','utf-8') 

msg['Subject'] = subject 

smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

各个成分都蕴涵的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 

# Create message container - the correct MIME type is multipart/alternative 
msg = MIMEMultipart('alternative') 
msg['Subject'] = "Link" 

# Create the body of the message (a plain-text and an HTML version) 
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://wwwpythonorg" 
html = """\ 
<html> 
 <head></head> 
 <body> 
  <p>Hi!<br> 
    How are you?<br> 
    Here is the <a href="http://wwwpythonorg">link</a> you wanted 
  </p> 
 </body> 
</html> 
""" 

# Record the MIME types of both parts - text/plain and text/html 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 

# Attach parts into message container 
# According to RFC 2046, the last part of a multipart message, in this case 
# the HTML message, is best and preferred 
msgattach(part1) 
msgattach(part2) 
#构造附件 
att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="jpg"' 
msgattach(att) 

smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

基于SSL的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
from emailheader import Header 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 

msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 

smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtpehlo() 
smtpstarttls() 
smtpehlo() 
smtpset_debuglevel(1) 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit()  

如上就是本文的全部内容,希望对大家的求学抱有援助,也可望我们多多帮助帮客之家。

发送邮件实例代码,python发送邮件
python 发送邮件实例 文件情势的邮件 #!/usr/bin/env python3 #coding:
utf-8 import smtplib from emailmimetext i…

python发送邮件,python3发送邮件

邮件发送系统大概流程

为了更加好的领会邮件发送功用的兑现,要先通晓邮件发送系统的大致流程。首先
 电子邮件之间的相互发送接受就好像 邮局邮件发送同样,从二个站点(邮件发送服务器)到指标地站点(邮件接收服务器),然后目标地站点管理收到的邮件,并发送给接受人。每种邮件服务器即出任发送也充当接受邮件,并且各个服务器地址,端口号,配置等也比不上。

其实发送邮件的流水生产线就 两部:

第一:写邮件

  • 写好发送方,接受方
  • 写好主题
  • 写好正文(包蕴附属类小部件,图片等)
  • 把信件整理在一块儿

其次:发送邮件

  • 连日来发送邮件服务器
  • 登入邮箱
  • 出殡邮件
  • 剥离邮箱

代码部分:

# -*- coding:utf-8 -*-
import smtplib
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#创建邮件对象,相当于信纸
msg = MIMEMultipart()
#邮件发送方
msg['From'] = 'XXXXXXXX.qq.com'
#邮件接收方
msg['To'] = '[email protected]'
#邮件主题
msg['Subject'] = Header('这是邮箱主题部分', charset='UTF-8')#中文主题

#文本内容
txt = '亲爱的用户'
part1 = MIMEText(txt, 'plain', 'utf-8')
#添加到邮件中
msg.attach(part1)

#超文本内容,放有图片,注意src路径,此为放在同一目录中
html = '<div><img src="cid:image1">亲爱的</div>'
#一定要设置邮件格式,否则可能会乱码,当时接手别人手中没加邮件格式的项目,坑了我半天
part2 = MIMEText(html, 'html', 'utf-8')
#添加到邮件中
msg.attach(part2)

#图片放到邮件中
# 指定图片为当前目录
fp = open('test.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)

#加入附件
att2 = MIMEText('这是附件里的文字', 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
#设置附件名为runoob.txt
att2["Content-Disposition"] = 'attachment; filename="runoob.txt"'
#附件放在邮件中
msg.attach(att2)

#非ssl方式的smtp(简单邮件发送协议),下面注释为ssl方式的smtp
smtp = smtplib.SMTP()
# smtp = smtplib.SMTP_SSL()

try:
    #连接主机地址
    smtp.connect('mail.yto.net.cn:25')
    #登录邮箱,需要邮箱账号,密码
    smtp.login('XXXXXXXX.qq.com','234123')
    #发送邮件,需要登录的邮箱号,发送方的邮箱号,发送内容
    smtp.sendmail('XXXXXXXX.qq.com', '[email protected]', msg.as_string())
    #退出邮箱
    smtp.quit()
    print 'The mail is sended successly.'
except smtplib.SMTPException,e:
    print "Error: 无法发送邮件",e

出殡结果:

奥门威尼斯网址 3

 

注意点:当自身邮箱作为邮件发送方时,注意邮件服务器的安装里把smtp服务张开

奥门威尼斯网址 4

邮件发送系统大约流程
为了越来越好的明白邮件发送作用的兑现,要先理解邮件发送系统的大概流程。首先
电子…

import smtplib 

 

from email.mime.text import MIMEText 

 

 

 

# 定义发送列表 

 

mailto_list=[“[email protected]”,”[email protected]”] 

 

 

 

# 设置服务器名称、客商名、密码以及邮件后缀 

 

mail_host = “smtp.163.com” 

 

mail_user =
“[email protected]” 

 

mail_pass = “xx” 

 

mail_postfix=”163.com” 

 

 

———–2016-5-11 15:03:58–

# 发送邮件函数 

  source:python发送邮件实例

def send_mail(to_list, sub, context): 

    ””’

    to_list: 发送给哪个人

    sub: 主题

    context: 内容

   
send_mail(“[email protected]”,”sub”,”context”)

    ”’ 

    me = mail_user + “<“+mail_user+”@”+mail_postfix+”>” 

    msg = MIMEText(context) 

    msg[‘Subject’] = ‘python email test’ 

    msg[‘From’] = sub

    msg[‘To’] = “;”.join(to_list) 

    try: 

        send_smtp = smtplib.SMTP() 

        send_smtp.connect(mail_host) 

        send_smtp.login(mail_user, mail_pass) 

        send_smtp.sendmail(me, to_list, msg.as_string()) 

        send_smtp.close() 

        print ‘success’

        return True 

    except (Exception, e): 

        print(str(e)) 

        print ‘false’

 

send_mail(mailto_list,”test mail”,”你好”)

导入 smtplib 和 MIMEText import smtplib from
email.mime.text import MIMEText # 定义发送列表
mailto_list=[[email protected],[email protected]]
# 设置服务器名称、客户名…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图