博客主页 🐟
C#-发送邮件(验证码)
C#-发送邮件(验证码)

Author:

©

Wordage:

共计 1261 字

needs:

约 1 分钟

Popular:

345 ℃

Created:

目 录

主代码:

MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("[email protected]");
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("[email protected]", "密钥");
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;
}
文章二维码
C#-发送邮件(验证码)
共计 0 条评论,点此发表评论
博客主页 哀.net OωO
萌ICP备20238808号 数字生命计划 本站已运行 1 年 272 天 11 小时 30 分 Copyright © 2023 ~ 2024. 哀.net All rights reserved.
打赏图
打赏博主
欢迎
搜 索
足 迹
分 类
  • 默认分类
  • 语言分类
  • 其他分类