Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (89 loc) · 3.72 KB
/
Copy pathProgram.cs
File metadata and controls
103 lines (89 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace MailVerifySDK
{
internal class Program
{
// 配置SMTP账号、发信人邮箱和授权码
private static readonly string SmtpAccount = "your_smtp_account@qq.com"; // 替换为你的SMTP账号
private static readonly string SmtpPassword = "your_smtp_password"; // 替换为你的SMTP授权码
private static readonly string FromEmail = "your_email@qq.com"; // 替换为你的发信人邮箱
static void Main(string[] args)
{
string toEmail;
// 检查是否通过命令行参数传递了收件邮箱
if (args.Length >= 2 && args[0] == "-receive")
{
toEmail = args[1];
Console.WriteLine($"您通过命令行输入的收件邮箱地址为:{toEmail}");
}
else
{
// 如果没有传递命令行参数,则提示用户在控制台输入收件邮箱
Console.WriteLine("请输入收件邮箱地址:");
toEmail = Console.ReadLine();
}
// 生成4位随机验证码
string verificationCode = GenerateVerificationCode();
Console.WriteLine($"生成的验证码为:{verificationCode}(仅用于调试,实际使用时不应显示)");
// 发送邮件
SendVerificationEmail(toEmail, verificationCode);
// 用户最多可以输入验证码5次
const int maxAttempts = 5;
int attempts = 0;
while (attempts < maxAttempts)
{
Console.WriteLine("请输入您收到的验证码:");
string userCode = Console.ReadLine();
if (userCode == verificationCode)
{
Console.WriteLine("验证码正确!");
break;
}
else
{
attempts++;
Console.WriteLine($"验证码错误!您还有 {maxAttempts - attempts} 次机会。");
}
}
if (attempts == maxAttempts)
{
Console.WriteLine("您已达到最大尝试次数,程序将退出。");
}
Console.WriteLine("按任意键退出...");
Console.ReadKey();
}
// 生成4位随机数字验证码
private static string GenerateVerificationCode()
{
Random random = new Random();
int code = random.Next(1000, 10000); // 生成1000到9999之间的随机数
return code.ToString();
}
// 发送验证邮件
private static void SendVerificationEmail(string toEmail, string verificationCode)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(FromEmail);
mail.To.Add(toEmail);
mail.Subject = "验证码邮件";
mail.Body = $"[MailVerifySDK]您的验证码是:{verificationCode},请在5分钟内完成验证哦(。^▽^)";
mail.IsBodyHtml = false;
SmtpClient smtpClient = new SmtpClient("smtp.qq.com", 587); // QQ邮箱SMTP服务器
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(SmtpAccount, SmtpPassword);
smtpClient.Send(mail);
Console.WriteLine("验证码邮件已发送!");
}
catch (Exception ex)
{
Console.WriteLine($"发送邮件时发生错误:{ex.Message}");
}
}
}
}