64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
import smtplib
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
import sys
|
|
|
|
def send_test_email(host='localhost', port=2525, sender='test@example.com', recipient='user@example.com'):
|
|
"""发送测试邮件"""
|
|
|
|
# 创建邮件内容
|
|
msg = MIMEMultipart()
|
|
msg['From'] = sender
|
|
msg['To'] = recipient
|
|
msg['Subject'] = '测试邮件 - 邮箱系统'
|
|
|
|
# 添加正文
|
|
body = """
|
|
这是一封测试邮件。
|
|
|
|
邮箱系统测试。
|
|
|
|
验证码: TEST123
|
|
验证链接: http://example.com/verify?code=123456
|
|
"""
|
|
msg.attach(MIMEText(body, 'plain'))
|
|
|
|
try:
|
|
# 连接SMTP服务器
|
|
server = smtplib.SMTP(host, port)
|
|
server.set_debuglevel(1) # 启用调试
|
|
|
|
# 发送邮件
|
|
server.sendmail(sender, recipient, msg.as_string())
|
|
|
|
# 关闭连接
|
|
server.quit()
|
|
|
|
print(f"邮件发送成功: {sender} -> {recipient}")
|
|
return True
|
|
except Exception as e:
|
|
print(f"邮件发送失败: {str(e)}")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
# 获取命令行参数
|
|
host = 'localhost'
|
|
port = 2525
|
|
sender = 'test@example.com'
|
|
recipient = 'user@example.com'
|
|
|
|
# 处理命令行参数
|
|
if len(sys.argv) > 1:
|
|
host = sys.argv[1]
|
|
if len(sys.argv) > 2:
|
|
port = int(sys.argv[2])
|
|
if len(sys.argv) > 3:
|
|
sender = sys.argv[3]
|
|
if len(sys.argv) > 4:
|
|
recipient = sys.argv[4]
|
|
|
|
print(f"发送测试邮件到SMTP服务器 {host}:{port}")
|
|
print(f"发件人: {sender}")
|
|
print(f"收件人: {recipient}")
|
|
|
|
send_test_email(host, port, sender, recipient) |