[c#]发送邮件验证码
主代码:
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("xxx@qq.com");
mailMessage.To.Add(new MailAddress(textBox1.Text));
mailMessage.Subject = "验证码:";
string verificationcode = createrandom(6);
mailMessage.Body = "你的验证码是:" + verificationcode;
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("xxx@qq.com", "密钥");
client.Send(mailMessage);
MessageBox.Show("发送成功!");
label1.Text = verificationcode;
随机生成验证码:
private string createrandom(int codelengh)
{
int rep = 0;
string str = string.Empty;
long num2 = DateTime.Now.Ticks + rep;
rep++;
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
for (int i = 0; i < codelengh; i++)
{
char ch;
int num = random.Next();
if ((num % 2) == 0)
{
ch = (char)(0x30 + ((ushort)(num % 10)));
}
else
{
ch = (char)(0x41 + ((ushort)(num % 0x1a)));
}
str = str + ch.ToString();
}
return str;
}