python发送邮件的实例代码(帮助html、图片、附属类

作者:电脑系统

message['From'] = from_addr

#!/usr/bin/env python3  
#coding: utf-8  
import smtplib  
from email.mime.text import MIMEText  
from email.header import Header  

smtpserver = 'smtp.163.com'

        #设定HTML信息
        msgText = MIMEText(htmlText, 'html', 'utf-8')
        msgAlternative.attach(msgText)

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

文件情势的邮件

to_addr= '********@qq.com'

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

sm.quit()

        #设定纯文本消息
        msgText = MIMEText(plainText, 'plain', 'utf-8')
        msgAlternative.attach(msgText)

#code:utf-8

首先段代码:

#att1=MIMEText(open('test.txt','rb').read(),'base64','utf-8')

复制代码 代码如下: #!/usr/bin/python # -*- coding: utf-8 -*- import email import mimetypes from email.MIMEMultipart import MIMEMultipart from email.MIMET...

smtp_user = '*******'

复制代码 代码如下:

#att1['Content-Type'] = 'application/octet-stream'

复制代码 代码如下:

#message = MIMEMultipart()

        if not (server and user and passwd) :
                print 'incomplete login info, exit now'
                return

#att1['Content-Disposition'] = 'attachment:filename="test.txt"'

复制代码 代码如下:

#注脚部分为带附属类小部件的代码

群邮件

#message.attach(att1)

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

import smtplib from email.mime.text import MIMEText

# 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.
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)

sm.sendmail(from_addr,to_addr,message.as_string())

        # 设定root信息
        msgRoot = MIMEMultipart('related')
        msgRoot['Subject'] = subject
        msgRoot['From'] = strFrom
        msgRoot['To'] = strTo
        msgRoot.preamble = 'This is a multi-part message in MIME format.'

from_addr='**********@163.com'

msg = MIMEText('你好','text','utf-8')#汉语需参数‘utf-8',单字节字符无需  
msg['Subject'] = Header(subject, 'utf-8')  

message['To'] = to_addr

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

sm=smtplib.SMTP()

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

from email.mime.multipart import MIMEMultipart

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

sm.login(smtp_user,smtp_pass)

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

sm.connect(smtpserver,25)

msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

#message.attach(message_text)

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

subject = '休假申请'

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

message = MIMEText('那是后天的假期申请','plain','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()

smtp_pass = '*******'

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

from email.header import Header

sender = '***'
receiver = ['***','****',……]
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

#协会附属类小部件
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)

msg['Subject'] = subject

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

基于SSL的邮件

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

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

msg['Subject'] = subject

复制代码 代码如下:

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText

msg = MIMEText('你好','plain','utf-8')#粤语需参数‘utf-8',单字节字符无需
msg['Subject'] = Header(subject, 'utf-8')

各个成分都包涵的邮件

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

复制代码 代码如下:

       #设定内置图片音信
        fp = open('test.jpg', 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<image1>')
        msgRoot.attach(msgImage)

def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

HTML方式的邮件

复制代码 代码如下:

        # Encapsulate the plain and HTML versions of the message body in an
        # 'alternative' part, so message agents can decide which they want to display.
        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)

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

带附属类小部件的邮件

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

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

fp = open('h:\python\1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib

带图片的HTML邮件

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

复制代码 代码如下:

        strFrom = fromAdd
        strTo = ', '.join(toAdd)

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText

        server = authInfo.get('server')
        user = authInfo.get('user')
        passwd = authInfo.get('password')

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

       #发送邮件
        smtp = smtplib.SMTP()
       #设定调解等级,依情形而定
        smtp.set_debuglevel(1)
        smtp.connect(server)
        smtp.login(user, passwd)
        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
        smtp.quit()
        return

#!/usr/bin/python
# -*- coding: utf-8 -*-

if __name__ == '__main__' :
        authInfo = {}
        authInfo['server'] = 'smtp.somehost.com'
        authInfo['user'] = 'username'
        authInfo['password'] = 'password'
        fromAdd = '[email protected]'
        toAdd = ['[email protected]', '[email protected]']
        subject = '邮件宗旨'
        plainText = '这里是平凡文本'
        htmlText = '<B>HTML文本</B>'
        sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)

sender = '***'  
receiver = '***'  
subject = 'python email test'  
smtpserver = 'smtp.163.com'  
username = '***'  
password = '***'  

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

复制代码 代码如下:

本文由分分快三计划发布,转载请注明来源

关键词: 分分快三计划