asp网站实现邮件的发送(注册为例)

来源:互联网 发布:父母从小偏心知乎 编辑:程序博客网 时间:2024/06/05 15:51
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;

using System.Net.Mail;
using System.Net;

public partial class register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
      


    }
    private string CheckCode()
    {
        int num;
        char code;
        string checkCode = string.Empty;
        Random random = new Random();
        for (int i = 0; i < 6; i++)//循环次数决定验证码的位数
        {
            num = random.Next();
            if (num % 2 == 0)
            {
                code = (char)('0' + (char)(num % 10));
            }
            else
            {
                code = (char)('A' + (char)(num % 26));
            }

            checkCode += code.ToString();
        }
        //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
        return checkCode;
    }
    protected void mailBind()
    {
        string lianjie = "http://localhost:2486/MailActivationOperation/register.aspx?code="+CheckCode();

        System.Net.Mail.MailMessage msg = new MailMessage();
        msg.Body = "欢迎您来到CSDN注册激活,您的激活码是" + CheckCode() +"<br/>"+ "点击链接可完成注册"+"<br/>" + lianjie;
        msg.From = new MailAddress("Leihaijiayuan@163.com");        //主机邮箱
        //msg.Sender = new MailAddress("");
        msg.To.Add(this.txtmail.Text);
        msg.Subject = "CSDN注册激活";
        msg.IsBodyHtml = true;  //邮件内容默认是纯文本!如果指定html内容,需要使用isbodyHtml


        SmtpClient sc = new SmtpClient();
        sc.Host = "smtp.163.com";           //主机邮箱类别
        sc.Port = 25;
        NetworkCredential nc = new NetworkCredential();
        nc.UserName = "leihaijiayuan";      //主机邮箱名称
        nc.Password = "*********";         //主机邮箱密码
        sc.Credentials = nc;
     
        sc.Send(msg);
        Response.Write("<script>alert('邮件已发送至您的邮箱,请去您的邮箱获取动态验证码!')</script>");

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
                    if (this.txtmail.Text != null&&this.txtkouling.Text=="")
            {
                mailBind();             
            }
            else if (this.txtmail.Text != null && this.txtkouling.Text != null)
            {
                string scc=Request["code"].ToString();
                if (this.txtkouling.Text == scc)
                {
                    string str = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
                    using (SqlConnection sqlcnn = new SqlConnection(str))
                    {
                        using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
                        {
                            sqlcmm.CommandText = "insert into register(users,sex,phone,pass,mail)values('"
                                + this.txtuser.Text + "','" + this.txtsex.Text + "','"
                                + this.txtnumber.Text + "','" + this.txtpass.Text + "','" + this.txtmail.Text + "')";
                            sqlcnn.Open();
                            sqlcmm.ExecuteNonQuery();
                            Response.Write("<script>alert('注册成功!')</script>");
                        }

                    }
                }
                else
                {
                    Response.Write("<script>alert('您的口令卡密码不正确,请确认!')</script>");
               
                }
            }
        
    }            

}

原创粉丝点击