|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ~风介~ 于 2015-11-4 13:29 编辑
原文出处:找不到了
①邮件发送
- #!/usr/bin/env python3
- #coding: utf-8
- import smtplib
- from email.mime.text import MIMEText
- from email.header import Header
-
- sender = 'abc@163.com' #发送邮箱名
- receiver = 'def@qq.com' #接收邮箱名
- subject = 'python email test !' #主题
- smtpserver = 'smtp.163.com' #邮件服务器
- username = 'abc' #昵称
- password = '123456' #密码(发送邮箱密码)
-
- msg = MIMEText('Hello Niko !','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
- msg['Subject'] = Header(subject, 'utf-8')
-
- smtp = smtplib.SMTP()
- smtp.connect('smtp.163.com')
- smtp.login(username, password)
- smtp.sendmail(sender, receiver, msg.as_string())
- smtp.quit()
复制代码
②带附件邮件发送
- #!/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
-
- sender = 'abc@163.com' #发送邮箱名
- receiver = 'def@qq.com' #接收邮箱名
- subject = 'python email test !' #主题
- smtpserver = 'smtp.163.com' #邮件服务器
- username = 'abc' #昵称
- password = '123456' #密码(发送邮箱密码)
-
- # Create message container - the correct MIME type is multipart/alternative.
- msg = MIMEMultipart('alternative')
- msg['Subject'] = "Link"
-
- # Create the body of the message (a plain-text and an HTML version).
- text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
- html = """\
- <html>
- <head></head>
- <body>
- <p>Hi!<br>
- How are you?<br>
- Here is the <a href="http://www.python.org">link</a> you wanted.
- </p>
- </body>
- </html>
- """
-
- # Record the MIME types of both parts - text/plain and text/html.
- part1 = MIMEText(text, 'plain')
- part2 = MIMEText(html, 'html')
-
- # 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('D:\\lover.jpg', 'rb').read(), 'base64', 'utf-8')
- att["Content-Type"] = 'application/octet-stream'
- att["Content-Disposition"] = 'attachment; filename="1.jpg"'
- msg.attach(att)
-
- smtp = smtplib.SMTP()
- smtp.connect('smtp.163.com')
- smtp.login(username, password)
- smtp.sendmail(sender, receiver, msg.as_string())
- smtp.quit()
复制代码
③html格式邮件发送
- #!/usr/bin/env python3
- #coding: utf-8
- import smtplib
- from email.mime.text import MIMEText
-
- sender = 'abc@163.com' #发送邮箱名
- receiver = 'def@qq.com' #接收邮箱名
- subject = 'python email test !' #主题
- smtpserver = 'smtp.163.com' #邮件服务器
- username = 'abc' #昵称
- password = '123456' #密码(发送邮箱密码)
-
- msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')
-
- msg['Subject'] = subject
-
- smtp = smtplib.SMTP()
- smtp.connect('smtp.163.com')
- smtp.login(username, password)
- smtp.sendmail(sender, receiver, msg.as_string())
- smtp.quit()
复制代码
④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
-
- sender = 'abc@163.com' #发送邮箱名
- receiver = 'def@qq.com' #接收邮箱名
- subject = 'python email test !' #主题
- smtpserver = 'smtp.163.com' #邮件服务器
- username = 'abc' #昵称
- password = '123456' #密码(发送邮箱密码)
-
- msgRoot = MIMEMultipart('related')
- msgRoot['Subject'] = 'test message'
-
- #构造附件
- att = MIMEText(open('D:\\lover.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()
复制代码 ⑥html格式带附件V图片邮件发送
- #!/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
-
- sender = 'abc@163.com' #发送邮箱名
- receiver = 'def@qq.com' #接收邮箱名
- subject = 'python email test !' #主题
- smtpserver = 'smtp.163.com' #邮件服务器
- username = 'abc' #昵称
- password = '123456' #密码(发送邮箱密码)
-
- msgRoot = MIMEMultipart('related')
- 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('D:\\lover.jpg', 'rb')
- msgImage = MIMEImage(fp.read())
- fp.close()
-
- msgImage.add_header('Content-ID', '<image1>')
- msgRoot.attach(msgImage)
-
- smtp = smtplib.SMTP()
- smtp.connect('smtp.163.com')
- smtp.login(username, password)
- smtp.sendmail(sender, receiver, msgRoot.as_string())
- smtp.quit()
复制代码
|
评分
-
查看全部评分
|