python发送邮件实例1

文件形式的邮件

 1 #!/usr/bin/env python3
 2 #coding: utf-8
 3 import smtplib
 4 from email.mime.text import MIMEText
 5 from email.header import Header
 6
 7 sender = ‘***‘
 8 receiver = ‘***‘
 9 subject = ‘python email test‘
10 smtpserver = ‘smtp.163.com‘
11 username = ‘***‘
12 password = ‘***‘
13
14 msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)#中文需参数‘utf-8’,单字节字符不需要
15 msg[‘Subject‘] = Header(subject, ‘utf-8‘)
16
17 smtp = smtplib.SMTP()
18 smtp.connect(‘smtp.163.com‘)
19 smtp.login(username, password)
20 smtp.sendmail(sender, receiver, msg.as_string())
21 smtp.quit()  

HTML形式的邮件

 1 #!/usr/bin/env python3
 2 #coding: utf-8
 3 import smtplib
 4 from email.mime.text import MIMEText
 5
 6 sender = ‘***‘
 7 receiver = ‘***‘
 8 subject = ‘python email test‘
 9 smtpserver = ‘smtp.163.com‘
10 username = ‘***‘
11 password = ‘***‘
12
13 msg = MIMEText(‘<html><h1>你好</h1></html>‘,‘html‘,‘utf-8‘)
14
15 msg[‘Subject‘] = subject
16
17 smtp = smtplib.SMTP()
18 smtp.connect(‘smtp.163.com‘)
19 smtp.login(username, password)
20 smtp.sendmail(sender, receiver, msg.as_string())
21 smtp.quit()  

带图片的HTML邮件

 1 #!/usr/bin/env python3
 2 #coding: utf-8
 3 import smtplib
 4 from email.mime.multipart import MIMEMultipart
 5 from email.mime.text import MIMEText
 6 from email.mime.image import MIMEImage
 7
 8 sender = ‘***‘
 9 receiver = ‘***‘
10 subject = ‘python email test‘
11 smtpserver = ‘smtp.163.com‘
12 username = ‘***‘
13 password = ‘***‘
14
15 msgRoot = MIMEMultipart(‘related‘)
16 msgRoot[‘Subject‘] = ‘test message‘
17
18 msgText = MIMEText(‘<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!‘,‘html‘,‘utf-8‘)
19 msgRoot.attach(msgText)
20
21 fp = open(‘h:\\python\\1.jpg‘, ‘rb‘)
22 msgImage = MIMEImage(fp.read())
23 fp.close()
24
25 msgImage.add_header(‘Content-ID‘, ‘<image1>‘)
26 msgRoot.attach(msgImage)
27
28 smtp = smtplib.SMTP()
29 smtp.connect(‘smtp.163.com‘)
30 smtp.login(username, password)
31 smtp.sendmail(sender, receiver, msgRoot.as_string())
32 smtp.quit()

带附件的邮件

 1 #!/usr/bin/env python3
 2 #coding: utf-8
 3 import smtplib
 4 from email.mime.multipart import MIMEMultipart
 5 from email.mime.text import MIMEText
 6 from email.mime.image import MIMEImage
 7
 8 sender = ‘***‘
 9 receiver = ‘***‘
10 subject = ‘python email test‘
11 smtpserver = ‘smtp.163.com‘
12 username = ‘***‘
13 password = ‘***‘
14
15 msgRoot = MIMEMultipart(‘related‘)
16 msgRoot[‘Subject‘] = ‘test message‘
17
18 #构造附件
19 att = MIMEText(open(‘h:\\python\\1.jpg‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
20 att["Content-Type"] = ‘application/octet-stream‘
21 att["Content-Disposition"] = ‘attachment; filename="1.jpg"‘
22 msgRoot.attach(att)
23
24 smtp = smtplib.SMTP()
25 smtp.connect(‘smtp.163.com‘)
26 smtp.login(username, password)
27 smtp.sendmail(sender, receiver, msgRoot.as_string())
28 smtp.quit() 

群邮件

 1 #!/usr/bin/env python3
 2 #coding: utf-8
 3 import smtplib
 4 from email.mime.text import MIMEText
 5
 6 sender = ‘***‘
 7 receiver = [‘***‘,‘****‘,……]
 8 subject = ‘python email test‘
 9 smtpserver = ‘smtp.163.com‘
10 username = ‘***‘
11 password = ‘***‘
12
13 msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)
14
15 msg[‘Subject‘] = subject
16
17 smtp = smtplib.SMTP()
18 smtp.connect(‘smtp.163.com‘)
19 smtp.login(username, password)
20 smtp.sendmail(sender, receiver, msg.as_string())
21 smtp.quit()  

各种元素都包含的邮件

 1 #!/usr/bin/env python3
 2 #coding: utf-8
 3 import smtplib
 4 from email.mime.multipart import MIMEMultipart
 5 from email.mime.text import MIMEText
 6 from email.mime.image import MIMEImage
 7
 8 sender = ‘***‘
 9 receiver = ‘***‘
10 subject = ‘python email test‘
11 smtpserver = ‘smtp.163.com‘
12 username = ‘***‘
13 password = ‘***‘
14
15 # Create message container - the correct MIME type is multipart/alternative.
16 msg = MIMEMultipart(‘alternative‘)
17 msg[‘Subject‘] = "Link"
18
19 # Create the body of the message (a plain-text and an HTML version).
20 text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
21 html = """\
22 <html>
23   <head></head>
24   <body>
25     <p>Hi!<br>
26        How are you?<br>
27        Here is the <a href="http://www.python.org">link</a> you wanted.
28     </p>
29   </body>
30 </html>
31 """
32
33 # Record the MIME types of both parts - text/plain and text/html.
34 part1 = MIMEText(text, ‘plain‘)
35 part2 = MIMEText(html, ‘html‘)
36
37 # Attach parts into message container.
38 # According to RFC 2046, the last part of a multipart message, in this case
39 # the HTML message, is best and preferred.
40 msg.attach(part1)
41 msg.attach(part2)
42 #构造附件
43 att = MIMEText(open(‘h:\\python\\1.jpg‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
44 att["Content-Type"] = ‘application/octet-stream‘
45 att["Content-Disposition"] = ‘attachment; filename="1.jpg"‘
46 msg.attach(att)
47
48 smtp = smtplib.SMTP()
49 smtp.connect(‘smtp.163.com‘)
50 smtp.login(username, password)
51 smtp.sendmail(sender, receiver, msg.as_string())
52 smtp.quit()  

基于SSL的邮件

 1 #!/usr/bin/env python3
 2 #coding: utf-8
 3 import smtplib
 4 from email.mime.text import MIMEText
 5 from email.header import Header
 6 sender = ‘***‘
 7 receiver = ‘***‘
 8 subject = ‘python email test‘
 9 smtpserver = ‘smtp.163.com‘
10 username = ‘***‘
11 password = ‘***‘
12
13 msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)#中文需参数‘utf-8’,单字节字符不需要
14 msg[‘Subject‘] = Header(subject, ‘utf-8‘)
15
16 smtp = smtplib.SMTP()
17 smtp.connect(‘smtp.163.com‘)
18 smtp.ehlo()
19 smtp.starttls()
20 smtp.ehlo()
21 smtp.set_debuglevel(1)
22 smtp.login(username, password)
23 smtp.sendmail(sender, receiver, msg.as_string())
24 smtp.quit()  
时间: 2024-08-17 22:11:53

python发送邮件实例1的相关文章

【转载】python发送邮件实例

本文转自:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html 这几天要用python发送邮件,上网找到这篇文章感觉蛮全面的,故转载收藏之. 1. 文件形式的邮件 #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header send

python 发送邮件实例

文本形式发邮件 #!/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 = '*

python发送邮件(一)

最近设计了一个小的应用程序,主要是根据文件中邮件地址发送一份excel中内容,并且在接受方收到邮件都是以网页的格式呈现的. 下面主要是对python发送邮件涉及到的部分知识点做个总结 一.先介绍一下Smtp协议和POP3协议 SMTP (Simple Mail Transfer Protocol) http://www.rfc-editor.org/info/rfc821    RFC821文档详细描述了这个协议信息: 邮件传送代理 (Mail Transfer Agent,MTA) 程序使用S

python 编程实例 1

#python 100 例 1.py #题目:有 1.2.3.4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多 #少? a = {} c = 1 for i in range(1,5): for j in range(1,5): for k in range(1,5): if (i != j,i !=k ,j!= k): #                print (i,j,k) a[c]=(i,j,k) c = c + 1 print (a) #把结果输入到字典 a中,并用c记数

python 编程实例 2

#python 100 2.py #题目:企业发放的奖金根据利润提成.利润 (I)低于或等于 10 万元时,奖金可提 10%:利 #润高 于 10 万元,低于 20 万元时,低于 10 万元的部分按 10%提成,高于 10 万元的部分, #可可提  成 7.5%:20 万到 40 万之间时,高于 20 万元的部分,可提成 5%:40 万到 60 万之间 #时高于 40 万元的部分,可提成 3%:60 万到 100 万之间时,高于 60 万元的部分,可提成 #1.5%,高于 100 万元时,超过

python 编程实例 3

#python 100 例 3.py #题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数.求这个数. import math for x in range(1,100000): y = int(math.sqrt(x + 100)) z = int(math.sqrt(x + 268)) if ( x + 100 == y*y ) and ( x + 268 == z*z): print (x) python 编程实例 3,布布扣,bubuko.com

python 编程实例 4

#python 100例 4.py #输入一个日期,判断这一天是一年中的第几天. import time #print (time.strftime("%Y%m%d%H%M%S")) #当前时间 #print (time.time()) #当前时间的秒数,从1970年1月1日开始计算 b = input("输入一个日期如(20121012): ") #输入要计算的日期 a = b[0:4]+'0101' #获取输入日期的年份并加上1月1日,从当年的1月1日开始计算

python 编程实例 5

#题目:输入三个整数 x,y,z,请把这三个数由小到大输出. #1.程序分析:我们想办法把最小的数放到 x 上,先将 x 与 y 进行比较,如果 x>y 则将 x 与 y #的值交换,再比较X 和Z比较. x = int(input("输入一个正整数X:")) y = int(input("输入一个正整数Y:")) z = int(input("输入一个正整数Z:")) if x >y: if x > z: if y >z

python 编程实例 6

#python 100 例 6.py #输出9*9口决 for i in range(1,10): for j in range(1,10): a = i * j print (i ,"*",j ,"=",a ) python 编程实例 6,布布扣,bubuko.com