现在的位置: 首页 > 综合 > 正文

发送邮件

2014年02月09日 ⁄ 综合 ⁄ 共 4738字 ⁄ 字号 评论关闭

1.制作客户端邮件发送系统(winform版)

前台:

后台

        private void button1_Click(object sender, EventArgs e)
        {
            MailMessage msg = new MailMessage();
            msg.Subject = this.textBox2.Text;
            msg.Body = this.textBox3.Text;

            msg.From = new MailAddress("1059625015@qq.com");
            msg.To.Add(textBox1.Text);
            msg.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.qq.com"; //发件方服务器地止
            client.Port = 25;  //发件方端口

            NetworkCredential credential = new NetworkCredential();
            credential.UserName = "1059625015";
            credential.Password = "57483985shang146";

            client.Credentials = credential;  //把证书交给代理

            Attachment att = new Attachment(this.textBox4.Text);
            msg.Attachments.Add(att);
            client.Send(msg);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox4.Text = this.openFileDialog1.FileName;
            }
        }

2.实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。

login前台:

 <div>
       
        <table style="width:100%;">
            <tr>
                <td>
                    用户名:</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    密码:</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Email:</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="注册" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>

    </div>

login后台:

        protected void Button1_Click(object sender, EventArgs e)
        {
            string userName = TextBox1.Text.Trim();
            string pwd = TextBox2.Text.Trim();
            string email = TextBox3.Text.Trim();
            string activeCode = Guid.NewGuid().ToString().Substring(0,6);

            string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "insert into  T_Eamil (FUserName,FPassword,FEmail,FActive,FActiveCode) values 

(@name,@pwd,@email,@active,@activeCode) select @@IDENTITY";
            cmd.Parameters.Add(new SqlParameter("name", userName));
            cmd.Parameters.Add(new SqlParameter("pwd",pwd));
            cmd.Parameters.Add(new SqlParameter("email", email));
            cmd.Parameters.Add(new SqlParameter("active", false));
            cmd.Parameters.Add(new SqlParameter("activeCode", activeCode));
            int num=Convert.ToInt32(cmd.ExecuteScalar());
            sendMail(email, activeCode, num);

            Response.Redirect("loginMessage.aspx");
            conn.Close();
            conn.Dispose();
            
        }

        protected void sendMail(string email,string activeCode,int id)
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("1059625015@qq.com");
            msg.To.Add(email);
            msg.Subject = "请激活注册";

            StringBuilder contentBuilder = new StringBuilder();
            contentBuilder.Append("请单击以下连接完成激活!");
            contentBuilder.Append("<a href='http://localhost:2533/index.aspx?id=" + id.ToString() + "&activeCode=" 

+ activeCode + "'>激活</a>");
            msg.Body = contentBuilder.ToString();
            msg.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.qq.com";
            client.Port = 25;

            NetworkCredential credential = new NetworkCredential();
            credential.UserName = "1059625015";
            credential.Password = "57483985shang146";

            client.Credentials = credential;
            client.Send(msg);
        }

loginMessage前台

<div>
     恭喜您注册成功。。。。。。。。。。。。。
    </div>

index后台

 protected void Page_Load(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request["id"]);
            string activeCode = Request["activeCode"].ToString();
            int num = 0;
            string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from T_Eamil where Id=@id";
            cmd.Parameters.Add(new SqlParameter("id",id));
            SqlDataReader read=cmd.ExecuteReader();
            if (read.Read())
            {
                if (read.GetString(read.GetOrdinal("FActiveCode")) == activeCode)
                {
                    num = 1;
                }
                else
                {
                    num = 0;
                    Response.Write("验证码不正确");
                }
            }
            else
            {
                Response.Write("用户不存在!!");
            }
            
               
         
            conn.Close();
            conn.Dispose();

            if (num == 1)
            {
                SqlConnection conn1 = new SqlConnection(conStr);
                conn1.Open();
                SqlCommand cmd1 = conn1.CreateCommand();
                cmd1.CommandText = "update T_Eamil set FActive='True' where Id=@id1";
                cmd1.Parameters.Add(new SqlParameter("id1", id));
                cmd1.ExecuteNonQuery();
                conn1.Close();
                conn1.Dispose();
            }
        }

抱歉!评论已关闭.