邮箱验证码

Posted ly-03-04

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了邮箱验证码相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace PubilcHelper
{

    public class EMailHelperTwo
    {
        /// <summary>
        /// 邮箱验证 返回验证码
        /// </summary>
        /// <param name="JiShou">接受的邮箱账号</param>
        /// <param name="Count">验证码数量</param>
        /// <returns></returns>
        public string Send(string JiShou,int Count)
        {
            string YZM = CreateBlendCode(Count);
            //发件人地址
            MailAddress from = new MailAddress("发件人QQ邮箱");
            MailMessage message = new MailMessage();
            message.Body = "您本次的验证码为"+YZM;
            message.IsBodyhtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            //收件人地址
            message.To.Add(JiShou); //接收人地址 是个变量
            message.Subject = "邮箱验证";
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.From = from;
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Host = "smtp.qq.com";
            client.Port = 587;
            //邮箱账户和密码
            //邮箱和邮件授权码
            client.Credentials = new System.Net.NetworkCredential("发件人QQ邮箱", "授权码");
            try
            {
                client.Send(message);
                return YZM;
            }
            catch (Exception ex)
            {             
                string mssage = ex.ToString();
                return "";
            }
         
        }
        /// <summary>
        /// 这个是使用字母,数字混合
        /// </summary>
        /// <param name="VcodeNum"></param>
        /// <returns></returns>
        public  string CreateBlendCode(int codeCount)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
            string[] allCharArray = allChar.Split(,);
            string randomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(61);
                if (temp == t)
                {
                    return CreateBlendCode(codeCount);
                }
                temp = t;
                randomCode += allCharArray[t];
            }

            return randomCode;
        }


    }
}

以上是关于邮箱验证码的主要内容,如果未能解决你的问题,请参考以下文章

java实现邮件发送验证码

QQ邮箱验证码

C# “用户注册“板块开发 发送邮箱验证码

C# “用户注册“板块开发 发送邮箱验证码

qq邮箱为啥邮件发送失败重新发送验证码

Java实现邮箱发送验证码