源码如下:
//MVC验证码获取
//保存验证码
public static string InputVerificationCode = "";
protected void Button1_Click1(object sender, EventArgs e)
{
try
{
//随机数0~10产生六位验证码
Random random = new Random();
for (int i = 0; i < 6; i++)
{
InputVerificationCode += random.Next(10).ToString();
}
//设置发送方的邮件信息,例如使用网易的smtp
string smtpServer = "smtp.qq.com";//SMTP服务器
string mailFrom = "jfblackbird@foxmail.com";//登录用户名
string userpassword = "ltzmudrssqzfhihe";//登录密码
//邮件服务设置
SmtpClient smtpClient = new SmtpClient();
//指定电子邮件发送方式;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//指定SMTP服务器
smtpClient.Host = smtpServer;
//用户名和密码
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userpassword);
//发送邮件设置
//发送人和收件人
MailMessage mailMessage = new MailMessage(mailFrom, this.txt_eamil.Value);
//邮件主题
mailMessage.Subject = "";
//邮件内容
mailMessage.Body = "尊敬的用户您好!您正在操作注册众筹之家账号,验证码为:" + InputVerificationCode + "。如果不是本人操作,请勿泄露验证码!";
//正文编码
mailMessage.BodyEncoding = Encoding.UTF8;
//设置为HTML格式
mailMessage.IsBodyHtml = true;
//设置优先级
mailMessage.Priority = MailPriority.Low;
//发送邮件
smtpClient.Send(mailMessage);
}
catch (SmtpException ex)
{
//Response.Write("<script>alert('" + ex.Message + "')</script>");
//this.txt_accounts.Text = null;
//this.txt_pwd.Text = null;
//this.txt_repwd.Text = null;
//this.txt_youxiang.Text = null;
//this.TextBox1.Text = null;
}
}