python发送 smtp邮件代码

代码语言:python

所属分类:其他

代码描述:python发送 smtp邮件代码,请在python2.7下运行

代码标签: python smtp 发送 邮件

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
# -*- coding:utf-8 -*-
import smtplib
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email.header import Header
# 发件人地址,通过控制台创建的发件人地址
username = '***'
# 发件人密码,通过控制台创建的发件人密码
password = '***'
# 自定义的回复地址
replyto = '***'
# 收件人地址或是地址列表,支持多个收件人,最多30个
#receivers = ['xxx@alx.com', 'xxx@xx.com']
#rcptto = ','.join(rcptto)
rcptto = '***'
# 构建alternative结构
msg = MIMEMultipart('alternative')
msg['Subject'] = Header('自定义信件主题'.decode('utf-8')).encode()
msg['From'] = '%s <%s>' % (Header('自定义发信昵称'.decode('utf-8')).encode(), username)
msg['To'] = rcptto
msg['Reply-to'] = replyto
msg['Message-id'] = email.utils.make_msgid()
msg['Date'] = email.utils.formatdate() 
# 构建alternative的text/plain部分
textplain = MIMEText('自定义TEXT纯文本部分', _subtype='plain', _charset='UTF-8')
msg.attach(textplain)
# 构建alternative的text/html部分
texthtml = MIMEText('自定义HTML超文本部分', _subtype='html', _charset='UTF-8')
msg.attach(texthtml)
# 发送邮件
try:
    client = smtplib.SMTP()
    #python 2.7以上版本,若需要使用SSL,可以这样创建client
    #client = smtplib.SMTP_SSL()
    #SMTP普通端口为25或80
    client.connect('smtpdm.ali.........完整代码请登录后点击上方下载按钮下载查看

网友评论0