qq邮箱怎么登录电脑,qq邮箱如何登录步骤

首页 > 实用技巧 > 作者:YD1662023-04-29 14:41:54

3. 发送文本格式的邮件

import smtplib from email.Header import Header from email.mime.text import MIMEText 代码块123

fromAddr ='zhangsan@qq.com' password = 'password for smtp' toAddr ='zhangsan@qq.com' subject = 'hello world' content = '你好,世界' 代码块12345

def makeMail(): mail = MIMEText(content, 'plain', 'utf-8') mail['From'] = Header(fromAddr, 'utf-8') mail['To'] = Header(toAddr, 'utf-8') mail['Subject'] = Header(subject, 'utf-8') return mail 代码块123456

def sendMail(mail): server = smtplib.SMTP_SSL("smtp.qq.com") server.login(fromAddr, password) server.sendmail(fromAddr, toAddr, mail.as_string()) server.quit() 代码块12345

mail = makeMail() sendMail(mail) print('发送邮件成功') 代码块123

运行程序,在 QQ 邮箱中收到邮件:

qq邮箱怎么登录电脑,qq邮箱如何登录步骤(5)

4. 发送 html 格式的邮件

import smtplib from email.header import Header from email.mime.text import MIMEText 代码块123

fromAddr ='zhangsan@qq.com' password = 'password for smtp' toAddr ='zhangsan@qq.com' subject = 'hello with html content' content = '<b>Please click</b> <a href="https://www.imooc.com">imooc</a>' 代码块12345

def makeMail(): mail = MIMEText(content, 'html', 'utf-8') mail['From'] = Header(fromAddr, 'utf-8') mail['To'] = Header(toAddr, 'utf-8') mail['Subject'] = Header(subject, 'utf-8') return mail 代码块123456

def sendMail(mail): server = smtplib.SMTP_SSL("smtp.qq.com") server.login(fromAddr, password) server.sendmail(fromAddr, toAddr, mail.as_string()) server.quit() 代码块12345

mail = makeMail() sendMail(mail) print('发送邮件成功') 代码块123

运行程序,在 QQ 邮箱中收到邮件:

qq邮箱怎么登录电脑,qq邮箱如何登录步骤(6)

5. 发送带有附件的邮件5.1 附件 hello.c

在当前目录下,创造文件 hello.c,内容如下:

#include <stdio.h> int main() { printf("hello\n"); return 0; } 代码块12345675.2 发送带有附件的邮件

import smtplib from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart 代码块1234

fromAddr ='zhangsan@qq.com' password = 'password for smtp' toAddr ='zhangsan@qq.com' subject = 'hello with attachment' content = '你好,请查收附件' 代码块12345

def makeAttachment(filename): file = open(filename, 'rb') blob = file.read() attachment = MIMEText(blob, 'base64', 'utf-8') attachment["Content-Type"] = 'application/octet-stream' attachment["Content-Disposition"] = 'attachment; filename="%s"' % filename return attachment 代码块1234567

def makeMail(): mail = MIMEMultipart() mail['From'] = Header(fromAddr, 'utf-8') mail['To'] = Header(toAddr, 'utf-8') mail['Subject'] = Header(subject, 'utf-8') mail.attach(MIMEText(content, 'plain', 'utf-8')) return mail 代码块1234567

def sendMail(mail): server = smtplib.SMTP_SSL("smtp.qq.com") server.login(fromAddr, password) server.sendmail(fromAddr, toAddr, mail.as_string()) server.quit() 代码块12345

mail = makeMail() attachment = makeAttachment('hello.c') mail.attach(attachment) sendMail(mail) print('发送邮件成功') 代码块12345

运行程序,在 QQ 邮箱中收到邮件:

qq邮箱怎么登录电脑,qq邮箱如何登录步骤(7)

欢迎关注「慕课网」,发现更多IT圈优质内容,分享干货知识,帮助你成为更好的程序员!

,
上一页12末页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.