//引入邮件服务包 using System.Net.Mail; using System.Net;
这两个引用用于C#接入邮件的SMTP服务
控件页面 定时器为了给用户更好的体验,我们使用定时器,给用户看到具体剩余时间
开始创建一个功能类新建一个类MailVeriCodeClass,里面定义好各类功能函数
首先引入必要的命名空间
//发送邮件需要引用的命名空间 using System.Net.Mail; using System.Net; //正则验证引用 using System.Text.Regularexpressions;功能类代码
public class MailVeriCodeClass
{
#region 邮箱验证码功能
///
/// 生成随机验证码
///
/// 验证码长度
public static string CreateRandomMailCode(int CodeLength)
{
int randNum;
char code;
string randomCode = String.Empty;//随机验证码
//生成一定长度的随机验证码
//Random random = new Random();//生成随机数对象
for (int i = 0; i < CodeLength; i++)
{
//利用GUID生成6位随机数
byte[] buffer = Guid.NewGuid().ToByteArray();//生成字节数组
int seed = BitConverter.ToInt32(buffer, 0);//利用BitConvert方法把字节数组转换为整数
Random random = new Random(seed);//以生成的整数作为随机种子
randNum = random.Next();
//randNum = random.Next();
if (randNum % 3 == 1)
{
code = (char)('A' + (char)(randNum % 26));//随机大写字母
}
else if (randNum % 3 == 2)
{
code = (char)('a' + (char)(randNum % 26));//随机小写字母
}
else
{
code = (char)('0' + (char)(randNum % 10));//随机数字
}
randomCode += code.ToString();
}
return randomCode;
}
///
/// 发送邮件验证码
///
/// 发件人邮箱地址
/// 收件人邮箱地址
/// 邮件主题
/// 邮件内容
/// 邮箱授权码
///
public static bool SendMailMessage(string MyEmailAddress, string RecEmailAddress, string Subject, string Body, string AuthorizationCode)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(MyEmailAddress);//发件人邮箱地址
mail.To.Add(new MailAddress(RecEmailAddress));//收件人邮箱地址
mail.Subject = Subject;//邮件标题
mail.Body = Body; //邮件内容
mail.Priority = MailPriority.High;//优先级
SmtpClient client = new SmtpClient();//qq邮箱:smtp.qq.com;126邮箱:smtp.126.com
client.Host = "smtp.qq.com";
client.Port = 587;//SMTP端口465或587
client.EnableSsl = true;//使用安全加密SSL连接
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(MyEmailAddress, AuthorizationCode);//验证发件人身份(发件人邮箱,邮箱授权码);
try
{
client.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "发送失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
///
/// 验证QQ邮箱
///
/// 邮箱
///
public static bool CheckMail(string mail)
{
string str = @"^[1-9][0-9]{4,}@qq.com$";
Regex mReg = new Regex(str);
if (mReg.IsMatch(mail))
{
return true;
}
return false;
}
#endregion
}
功能类已经写好了,每个方法对应有注释,不需要动,除非你要更改smtp服务器,默认QQ邮箱
窗体类代码首先定义全局
////// 定义控件全局 /// int seconds1 = 60;//倒计时60s int seconds2 = 60 * 2;//验证码有效时间5分钟 string strMailVeriCode;//验证码
获取验证码后计时事件
////// 按钮控件获取验证码后计时事件 /// /// /// private void timer1_Tick(object sender, EventArgs e) { if (seconds1 > 0) { seconds1--; btnMailVeriCode.Text = "剩余" + seconds1.ToString() + "秒"; } else { timer1.Stop(); btnMailVeriCode.Text = "获取验证码"; btnMailVeriCode.Enabled = true; } }
此事件为第一个timer控件的事件
验证码过期后事件
////// 验证码过期事件 /// /// /// private void timer2_Tick_1(object sender, EventArgs e) { if (seconds2 == 0) { timer2.Stop(); //旧的验证码过期,生成一个新的验证码 strMailVeriCode = MailVeriCodeClass.CreateRandomMailCode(6); } }
此事件为第二个timer控件的事件
点击获取验证码(按钮点击事件)
////// 获取验证码点击事件 /// /// /// private void btnMailVeriCode_Click_1(object sender, EventArgs e) { string recEMailAddress = txtMail.Text.Trim();//收件人邮箱 strMailVeriCode = MailVeriCodeClass.CreateRandomMailCode(6); string strBody = "验证码:" + strMailVeriCode + ",2分钟内有效,请勿泄漏于他人。如非本人操作,请忽略。系统邮件请勿回复。";//邮件内容 string strSubject = "【极简科技】注册验证";//邮件标题 string strMyEmailAddress = "747945307@qq.com";//发件人邮箱 string strAuthorizationCode = "klxtvclkyfu*****";//邮箱授权码 if (string.IsNullOrEmpty(recEMailAddress))//判断是否输入了邮箱 { MessageBox.Show("请输入邮箱!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtMail.Focus(); } else if (MailVeriCodeClass.CheckMail(recEMailAddress) == false)//判断邮箱格式是否正确 { MessageBox.Show("您输入的QQ邮箱有误,请重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtMail.Focus(); return; } else//发送验证码 { //发送 if (MailVeriCodeClass.SendMailMessage(strMyEmailAddress, recEMailAddress, strSubject, strBody, strAuthorizationCode) == true) { btnMailVeriCode.Enabled = false; //计时器初始化 timer1.Interval = 1000; timer1.Start(); timer2.Interval = 1000; timer2.Start(); } else { txtMail.Focus(); } } }
在这三句代码配置好SMTP服务
string strSubject = "【极简科技】注册验证";//邮件标题 string strMyEmailAddress = "747945307@qq.com";//发件人邮箱 string strAuthorizationCode = "klxtvclkyfu*****";//邮箱授权码
但是注意,这里的邮箱授权码不是邮箱密码,而是SMTP授权码!
SMTP开启并获取授权码
到这然后往下看
以QQ邮箱为例,首先开启上面箭头所指的SMTP服务,随便开一个都可以
然后点击下面箭头指向的生成授权码,这个就是你的SMTP授权码
验证验证码是否正确
////// 验证码确认按钮点击事件 /// /// /// private void btn/confirm/i_Click_1(object sender, EventArgs e) { string mailVeriCode = txtMailVeriCode.Text.Trim();//邮箱验证码 //判断验证码 if (string.IsNullOrEmpty(mailVeriCode) == true) { MessageBox.Show("请输入验证码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtMailVeriCode.Focus(); } else if (mailVeriCode.ToLower() != strMailVeriCode.ToLower())//判断邮箱验证码是否输入正确;不区分字母大小写 { MessageBox.Show("您输入的验证码有误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtMailVeriCode.Focus(); return; } else { MessageBox.Show("验证成功!"); } }
这里是确认按钮的点击事件,判断验证码是否准确无误
运行实例到这就完成了,看看效果



