Advertisement
Jh0n-K3v1n

Envio Correo

Apr 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Text;
  9. using System.Configuration;
  10. using System.Net.Mail;
  11. using System.Net;
  12. using MySql.Data.MySqlClient;
  13. //using SimpleCrypto;
  14.  
  15.  
  16. namespace SoftwareChampions_Col.Vista
  17. {
  18.     public partial class recuperarcontraseña : System.Web.UI.Page
  19.     {
  20.         MySqlConnection con = new MySqlConnection(@"Data Source=localhost; Database=championscol; User ID=root; Password=123456789");
  21.  
  22.         protected void Page_Load(object sender, EventArgs e)
  23.         {
  24.             if (!IsPostBack)
  25.             {
  26.                 Label1.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  27.  
  28.             }
  29.  
  30.             Button2.Visible = false;
  31.             Button3.Visible = false;
  32.             Label1.Visible = false;
  33.             Label2.Visible = false;
  34.             TextBox2.Visible = false;
  35.            
  36.  
  37.         }
  38.         protected void ValidacionEmail()
  39.         {
  40.             string Correo = TextBox1.Text.Trim();
  41.             if (Correo == "")
  42.             {
  43.                 Label2.Visible = true;
  44.                 Label2.Text = "* Por favor debe de llenar el campo";
  45.             }
  46.             else
  47.             {
  48.                     con.Open();
  49.                     MySqlCommand cmd = con.CreateCommand();
  50.                     cmd.CommandType = CommandType.Text;
  51.                     cmd.CommandText = "select * from accounts where Email='" + TextBox1.Text + "'";
  52.                     cmd.ExecuteNonQuery();
  53.                     DataTable dt = new DataTable();
  54.                     MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  55.                     da.Fill(dt);
  56.                     foreach (DataRow dr in dt.Rows)
  57.                     {
  58.  
  59.                         if (dt.Rows.Count > 0)
  60.                         {
  61.                         Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Hemos enviado un codigo de recuperacion a su correo');</script>");
  62.                         //cmd.CommandText = "insert into accounts(CodigoVerficacion) values (?CodigoVerficacion)";
  63.                         EnviarCorreo(Correo);
  64.                         Response.Redirect("codigoverificacion.aspx");
  65.                     }
  66.                 }
  67.                 con.Close();
  68.                 Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Correo incorrecto');</script>");
  69.                
  70.             }
  71.            
  72.         }
  73.         public string GeneradorDeCodigos(int longitud)
  74.         {
  75.             string caracteres = "12J45K789T";
  76.             StringBuilder res = new StringBuilder();
  77.             Random rnd = new Random();
  78.             while (0 < longitud--)
  79.             {
  80.                 res.Append(caracteres[rnd.Next(caracteres.Length)]);
  81.             }
  82.             return res.ToString();
  83.         }
  84.  
  85.         protected void Button1_Click(object sender, EventArgs e)
  86.         {
  87.             ValidacionEmail();
  88.            
  89.         }
  90.  
  91.  
  92.         public void EnviarCorreo(string Destino)
  93.         {
  94.             string correo = TextBox1.Text;
  95.             string password = GeneradorDeCodigos(10);
  96.             string correoAdmin = ConfigurationManager.AppSettings["CorreoElectronico"].ToString();
  97.             string ContraAdmin = ConfigurationManager.AppSettings["ContraseñaCorreo"].ToString();
  98.  
  99.             string Asunto = "Recuperar contraseña Champions_Col[Servidor Privado]";
  100.             string Body = "su codigo de verificacion es " + password + " recuerde cambiar la contraseña por una facil de recordar.";
  101.  
  102.             var smtp = new SmtpClient();
  103.             {
  104.                 smtp.Host = "smtp-mail.outlook.com";
  105.                 smtp.Port = 587;
  106.                 smtp.EnableSsl = true;
  107.                 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  108.                 smtp.Credentials = new NetworkCredential(correoAdmin, ContraAdmin);
  109.                 smtp.Timeout = 10000;
  110.             }
  111.             try
  112.             {
  113.                 smtp.Send(correoAdmin, Destino, Asunto, Body);
  114.                 VaciarCampo();
  115.             }
  116.             catch (Exception)
  117.             {
  118.                 Response.Write("No se puedo enviar el email");
  119.             }
  120.            
  121.         }
  122.         void VaciarCampo()
  123.         {
  124.             TextBox1.Text = "";
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement