python的邮件模块smtplib&email

import smtplib
import string
from email.mime.text import MIMEText

def send_mail(host, sender, sender_passwd, receiver, content_file, port="25"):
    # print "create smtp object"
    server = smtplib.SMTP()
    # print "conncect smtp server..."
    server.connect(host, port)
    # print "login smtp server..."
    server.login(sender, sender_passwd)
    # print "read content file..."
    fp = open(content_file, ‘r‘)
    content = fp.read()
    fp.close()
    msg = MIMEText(content, "html", "utf-8")
    msg[‘Subject‘] = "BiaoTi"        # 标题也可以放进外部变量里,
    msg[‘From‘] = sender
    msg[‘To‘] = receiver
    try:
        server.sendmail(sender, receiver, msg.as_string())
        print "发送成功!"
    except Exception, e:
        print "发送失败:" + str(e)
    server.quit()

send_mail("smtp.xxxx.com", "[email protected]", "123456", "[email protected]", "mail.txt")

邮件内容文件(自写的html格式文件):

<h1>Hello World</h1>

<hr color="blue">

Nice to meet you, Henry.

<b> This is my first smtplib email.</b>

ok, say Hi.

Byebye

123456

<br />

654321

最后收到的邮件显示如下:

python的邮件模块smtplib&email

时间: 2024-08-23 16:21:53

python的邮件模块smtplib&email的相关文章

【Python】 发邮件用 smtplib &amp; email

■ smtplib & email ■ 概述 发邮件主要用到smtplib以及email模块.stmplib用于邮箱和服务器间的连接,发送的步骤.email模块主要用于处理编码,邮件内容等等.主要是参考了那个发报表的脚本,简单记录了下最简单的用法,没啥参考价值= = ■ smtplib基本用法 smtp = smtplib.SMTP() #建立smtp对象 smtp.connect('server',port) #建立与smtp服务器的连接 smtp.login('user','password

Python SMTP邮件模块

SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件. 实例: 1.使用Python发送纯文本格式和html格式的邮件. 1 #!/usr/bin/env python 2 #coding:utf-8 3 4 import smtplib 5 from email.mime.text import MIMEText 6 fr

Python使用SMTP模块、email模块发送邮件

smtplib模块: 主要通过SMTP类与邮件系统进行交互.使用方法如下: 1.实例化一个SMTP对象: s = smtplib.SMTP(邮件服务地址,端口号) s = smtplib.SMTP_SSL(邮件服务地址,端口号) 2.登陆邮件,权限验证: s.login(用户名,密码) 3.发送邮件: s.sendmail(发件人邮箱,收件人邮箱,发送内容) 4.断开连接: s.close() email模块: email模块:支持发送的邮件内容为纯文本.HTML内容.图片.附件.email模块

python使用电子邮件模块smtplib的方法(发送图片 附件)实用可行

Smptp类定义:smtplib.SMTP(host[,port[,local_hostname[,,timeout]]]),作为SMTP的构造函数,功能是与smtp服务器建立连接,在连接成功后,就可以向服务器发送相关请求,比如登陆,校验,发送,退出等.host参数为远程smtp主机地址,比如stmp.163.com;port为连接端口,默认为25:local_hostname的作用是在本地的FQDN(完整的域名)发送HELO/EHLO指令,timeout为连接或尝试在多数秒超时,SMTP类具有

python使用电子邮件模块smtplib

Smptp类定义:smtplib.SMTP(host[,port[,local_hostname[,,timeout]]]),作为SMTP的构造函数,功能是与smtp服务器建立连接,在连接成功后,就可以向服务器发送相关请求,比如登陆,校验,发送,退出等.host参数为远程smtp主机地址,比如stmp.163.com;port为连接端口,默认为25:local_hostname的作用是在本地的FQDN(完整的域名)发送HELO/EHLO指令,timeout为连接或尝试在多数秒超时,SMTP类具有

python 发送电子邮件模块smtplib

#!/usr/bin/python # -*- coding: utf-8 -*- import smtplib import string HOST = "smtp.uinx.com.cn" SUBJECT = "Test email from Python" #TO = "[email protected]" #TO = "[email protected]" TO = "[email protected]&qu

python发送电子邮件模块smtplib

一.简介: 电子邮件是最流行的互联网应用之一,在系统管理中,经常需要使用邮件来告警信息,业务质量报告等.方便运维人员在第一时间了解业务的服务状态,将通过使用python的smtplib模块来实现邮件的发送功能,能模拟一个smtp的客户端,通过与smtp服务器交互来实现邮件的发送功能,可以理解成foxmail的发邮件功能,在第一次使用的时候需要诶只smtp的主机地址,邮箱帐号密码等信息.python 2.4以上的版本默认就自带了smtplib模块,无需额外安装. 二.smtplib模块的常用类与方

Python网络协议模块学习之smtplib

功能:通过邮件服务器发送电子邮件,smtplib是smtp客户端的实现,支持邮件格式有:文本.HTML.Image.EXCEL 1.普通文本邮件 #!/usr/bin/env python # coding:UTF-8 import smtplib import string host = "smtp.qq.com"        #定义smtp主机 subject = "Test email from Python"      #定义邮件主题 to_mail = 

Python学习笔记-邮件模块SMTP

smtplib模块: SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一种很方便的途径发送电子邮件.它对smtp协议进行了简单的封装. Python创建 SMTP 对象语法如下: import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) 参数说明: hos