纯文本:

#!/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 = '***' msg=MIMEText(_text="你好",_charset='utf-8')#中文需参数‘utf-8’,单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From']=sender
msg["To"]=receiver
smtp = smtplib.SMTP()
smtp.connect('smtp.exmail.qq.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

 HTML格式:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***' msgRoot = MIMEMultipart()
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')
msgRoot.attach(msgText) fp = open('e:\\1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close() msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage) smtp = smtplib.SMTP()
smtp.connect('smtp.exmail.qq.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

 附件邮件:

#coding: utf-8    

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***' msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message' #构造附件
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】smtp邮件发送的更多相关文章

  1. python SMTP邮件发送(转载)

    Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...

  2. Python SMTP邮件发送

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块: email负责构造邮件 ...

  3. python学习笔记(SMTP邮件发送:带附件)

    博主有段时间没有更新博客了 先整理一个之前整理过的SMTP邮件发送,这次是带附件的功能 #!/usr/bin/env python # -*- coding: utf_8 -*- from email ...

  4. python学习笔记(SMTP邮件发送)

    想着给框架添加邮件发送功能.所以整理下python下邮件发送功能 首先python是支持邮件的发送.内置smtp库.支持发送纯文本.HTML及添加附件的邮件 之后是邮箱.像163.qq.新浪等邮箱默认 ...

  5. Python 基于Python实现邮件发送

    基于Python实现邮件发送   by:授客 QQ:1033553122 测试环境: Python版本:Python 2.7   注:需要修改mimetypes.py文件(该文件可通过文章底部的网盘分 ...

  6. 用Python实现邮件发送Hive明细数据

    代码地址如下:http://www.demodashi.com/demo/12673.html 一.需求描述 客户需要每周周一接收特定的活动数据,生成Excel或是CSV文件,并通过邮件发送给指定接收 ...

  7. pyqt5实现SMTP邮件发送

    # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'SMTP.ui' # # Created b ...

  8. Python SMTP邮件模块

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...

  9. python实现邮件发送完整代码(带附件发送方式)

    实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...

  10. python实现邮件发送

    实例补充: #**************************利用STMP自动发送邮件******************************import smtplibsmtp = smtp ...

随机推荐

  1. 如何在 OSX 中使用多个JDK版本

    升级macbook小白的硬盘成SSD后,重新安装了系统和JDK8,但是启动eclipse还是报告需要安装JDK6,于是也按照提示安装了Apple JDK6,这导致系统中有两个JDK,一个是Oracle ...

  2. pickle & cPickle ValueError: unsupported pickle protocol: 3

    pickle and cPickle pickle和cPickle是python对象的转储文件,保存的是python对象 他们分别是python2和python3的对应部分,建议引入的时候采用以下方法 ...

  3. C# .NET 开发心得

    1. 工作路径问题 1. 多项目构成的解决方案,Web APP作为启动项目时的工作路径 //当前执行的exe文件名 //C:\\Program Files\\IIS Express\\iisexpre ...

  4. ongene database

    http://ongene.bioinfo-minzhao.org/index.html

  5. BZOJ 2342: 【SHOI2011】 双倍回文

    题目链接:双倍回文 回文自动机第二题.构出回文自动机,那么一个回文串是一个“双倍回文”,当且仅当代表这个串的节点\(u\)顺着\(fail\)指针往上跳,可以找到一个节点\(x\)满足\(2len_x ...

  6. MySQL —— 基本查询方法

    MySQL —— 简单查询与按条件查询 在MySQL中从数据表中查询数据的基本语句时select语句.  select语句基本语法格式:      select 查询内容       from 表名  ...

  7. Linux命令详解-install

    install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户. 1.命令格式: (1)install [选项]... 来源 目的地 (2)install [选项]... 来源... 目录 ...

  8. 尺取法——POJ3061

    #include <iostream> //nlogn复杂度的写法 #include <cstdio> #include <algorithm> using nam ...

  9. 002——数组(二)each() list() implode() explode() in_array()

    <?php /** * 数组(二)each() list() implode() explode() in_array() */ /*implode() 把数组拆分成字符串 * explode( ...

  10. facade外观模式

    通过买股票与通过基金买股票引出外观模式: package com.disign.facade; /** * Created by zhen on 2017-05-18. */ public class ...