Python发邮件
PS: 也是从网上各个帖子中学习的Python,因此代码的格式以及内容有粘贴网上其他大神的代码,如有侵权请告知删除
示例图片
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 邮件模块
import smtplib
from email.mime.text import MIMEText
import time
from tkinter import *
import tkinter.filedialog
root = Tk()
root.geometry('400x400')
def SendMail():
# 163邮箱的地址
mail163Url = Sentence1.get()
# 163邮箱服务器的端口号
mail163Port = Sentence2.get().
# 发件箱地址
userMail = Sentence3.get()
# 授权密码,不是登录密码
passwd = Sentence4.get()
# 收件人地址
toMail = Sentence5.get()
# 连接邮箱服务器, 获得服务器对象
smtpServer = smtplib.SMTP(mail163Url, mail163Port)
# 登陆邮箱服务器
smtpServer.login(userMail, passwd)
# 编辑邮箱内容 — 收件人, 发件人, 主题…
mineText = MIMEText(Sentence7.get())
# 设置主题
mineText['Subject'] = Sentence6.get()
# 设置收件人
mineText['To'] = toMail
# 设置发件人
mineText['From'] = Sentence8.get()
# 发送邮件
for i in range(1, 6):
smtpServer.sendmail(userMail, toMail, mineText.as_string())
time.sleep(2)
# 关闭连接
smtpServer.close()
# -------------------------------------界面---------------------------------
Text1 = Label(root, text='邮箱类型:')
Text1.grid(row=1, sticky=W)
Sentence1 = Entry(root)
Sentence1.grid(row=1, column=1, sticky=W)
Text2 = Label(root, text='邮箱服务器端口号:')
Text2.grid(row=2, sticky=W)
Sentence2 = Entry(root)
Sentence2.grid(row=2, column=1, sticky=W)
Text3 = Label(root, text='登录邮箱名:')
Text3.grid(row=3, sticky=W)
Sentence3 = Entry(root)
Sentence3.grid(row=3, column=1, sticky=W)
Text4 = Label(root, text='登录邮箱授权码:')
Text4.grid(row=4, sticky=W)
Sentence4 = Entry(root)
Sentence4.grid(row=4, column=1, sticky=W)
Text5 = Label(root, text='收件人邮箱地址:')
Text5.grid(row=5, sticky=W)
Sentence5 = Entry(root)
Sentence5.grid(row=5, column=1, sticky=W)
Text6 = Label(root, text='邮件主题:')
Text6.grid(row=6, sticky=W)
Sentence6 = Entry(root)
Sentence6.grid(row=6, column=1, sticky=W)
Text7 = Label(root, text='邮件内容:')
Text7.grid(row=7, sticky=W)
Sentence7 = Entry(root)
Sentence7.grid(row=7, column=1, sticky=W)
Text8 = Label(root, text='显示发件人:')
Text8.grid(row=8, sticky=W)
Sentence8 = Entry(root)
Sentence8.grid(row=8, column=1, sticky=W)
Action = Button(root, text="执行", command=SendMail)
Action.grid(row=9, column=0, sticky=W)
root.mainloop()