You must Sign In to post a response.
  • Category: ASP.NET

    I need a code to send the user password to his e-mail

    i've a gridview contain the information of students who want to apply to my university. on my page i've this gridview showing their names and information below i write student name in a text box, his email also text box, and then i write him his password i type it by my own and then there's this SEND button, so when i press it i want to send him the password to his e-mail to be able to enter to the website. what is the code for that plzz???
  • #622520
    to send email you can use below code :

    using System.Net.Mail;
    using System.Configuration;
    public int SendMessage(string subject, string messageBody,string fromAddress, string toAddress)
    {
    MailMessage message = new MailMessage();
    message.IsBodyHtml = true;
    SmtpClient client = new SmtpClient();
    message.From = new MailAddress(fromAddress.ToString ());
    if (toAddress.Trim().Length > 0)
    {
    foreach (string addr in toAddress.Split(';'))//you can use any delimiter
    {
    message.To.Add(new MailAddress(addr));
    }
    }
    message.Subject = subject;
    message.Body = messageBody;
    client.Host = "SMTPServerName";
    client.Port = 25;
    client.UseDefaultCredentials = true;
    client.Credentials = new System.Net.NetworkCredential("mail id", "password");
    client.Send(message); return 1;
    }


    for more information about sending mail check my post here :
    http://dotnetsquare.com/resources/1-Easy-way-To-Send-Mail-with-without-attachment-Through-ASPNet-C-Net-VBNet

    If it helps you then Rate this.

    Best Regards,
    Manoranjan Sahoo
    http://www.dotnetsquare.com
    Blog: http://blog.msahoo.net

  • #622529
    Hi place one textbox and grid view in client side. One of the column is used Check box. If user select check box and enter password in the textbox then click send button. Check what are the rows are checked send mail based on this like below


    protected void Button1_Click(object sender, EventArgs e)
    {
    if (TextBox1.Text == "")
    {
    Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Enter Message in Text Box');</script>");
    return;
    }

    //Checkther whether atleast one check box is selected or not
    for(int i=0;i<=GridView1.Rows.Count-1;i++)
    {
    GridViewRow row = GridView1.Rows[i];
    CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
    if (Ckbox.Checked == true)
    {
    k++;
    }
    }

    if (k == 0)
    {
    Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Select atleast one Check box further to proceed');</script>");
    return;
    }

    for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
    {
    string toemail = GridView1.Rows[i].Cells[3].Text;
    GridViewRow row = GridView1.Rows[i];
    CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
    if (Ckbox.Checked == true)
    {
    sendMail(toemail);
    }
    }
    TextBox1.Text = "";
    }

    void sendMail(String toemail)
    {
    try
    {
    //Below I mention From id(Gmail), To mail ID, Subject of the mail, Mail Message
    MailMessage msg = new MailMessage("fromid@gmail.com", toemail , "Testing Mail", TextBox1.Text);
    SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
    //Change your gmail user id and password for send email
    NetworkCredential NetCrd = new NetworkCredential("fromid@gmail.com", "fromidpassword");
    mailClient.UseDefaultCredentials = false;
    mailClient.Credentials = NetCrd;
    mailClient.EnableSsl = true;
    mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    mailClient.Send(msg);
    }
    catch (Exception ex)
    {
    Response.Write(ex.ToString());
    }
    }


    Still u have any problem refer below resource with detailed explanation
    http://www.dotnetspider.com/resources/42891-How-send-email-for-selected-record-Grid.aspx

    Regards
    N.Ravindran
    Your Hard work never fails

  • #622559
    Hai Ameera,

    The requirement which you have is to send an email using asp.net and the data which you should get is from the grid.

    so as per the requirements, you can send the mail using the below code snippet but its always recomended to modify the code as per your requirments. But before that, let me refraze your requirement so that it will be esy for you to understand:-

    You can have the grid which contains all the information of all the students inlcluding their student id, name, email address etc. with that you can have one more template column called password which should be a textbox so that you can enter it or you can use any algorithm to generate it automatically..its all depend on you that how do you want to perform it.
    Okie..so now you have the grid with full information including the passowrd. In the first column of the grid, you can have a checkbox which will be usefull to sselect one or more than one students so that you can send the passowrd for any number of students in one shot.
    Now you need a button which you already have to send the mail.




    The first thing is that , you need to find out the checkbox with its checked status and the password textbox value.

    you can sue the below code to find it:

    for (int i = 0; i < gvStudentGrid.Rows.Count; i++)
    {
    string strstudentName = gvStudentGrid.Rows[i].Cells[2].Text;
    string strEmail = gvStudentGrid.Rows[i].Cells[4].Text;
    string strPassword = gvStudentGrid.Rows[i].Cells[5].Text;
    GridViewRow row = gvStudentGrid.Rows[i];
    CheckBox ChBox = (CheckBox)row.FindControl("chkCheck");
    if (ChBox.Checked == true)
    sendMailToStudent(strStudentName,strEmail, strPassword);
    }

    Now you can define the send mail mesthod he student:
    void sendMail(string studentName, string strStudentEmail, strPassword)
    {
    try
    {
    MailMessage msg = new MailMessage("fromid@ymail.com", strStudentEmail, studentName+ " Test Mail Message", strPassword);
    SmtpClient mailClient = new SmtpClient("smtp.ymail.com", 253);
    NetworkCredential crd = new NetworkCredential("fromid@gmail.com", "fromidpassword");
    mailClient.UseDefaultCredentials = false;
    mailClient.Credentials = crd;
    mailClient.EnableSsl = true;
    mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    mailClient.Send(msg);
    }
    catch (Exception ex)
    {
    Response.Write(ex.ToString());
    }
    }


    So you can sue the above code to send the mail as well.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.