Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Net;
public partial class forget : System.Web.UI.Page
{
    private object lblMessage;

    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            DataSet ds = new DataSet();
            String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                //SqlCommand cmd = new SqlCommand("SELECT UserName,Password FROM UserInfo Where Email= '" + txtEmail.Text.Trim() + "'", con);
                string Command = "select uname,pwd from register where email = @emailid and  Security_1 = @Security1 and Security_2 = @Security2";
                SqlCommand cmd = new SqlCommand(Command, con);
               
                cmd.Parameters.AddWithValue("@emailid", txt_email.Text.Trim());
                cmd.Parameters.AddWithValue("@Security1", txt_security1.Text);
                cmd.Parameters.AddWithValue("@Security2", txt_security2.Text);
                SqlDataAdapter da = new SqlDataAdapter(cmd);

                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txt_email.Text);
                // Recipient e-mail address.
                Msg.To.Add(txt_email.Text);
                Msg.Subject = "Your Password Details";
                Msg.Body = "Hi, <br>Please check your Login Detailss<br><br>Your Username: " + ds.Tables[0].Rows[0]["uname"] + "<br><br>Your Password: " + ds.Tables[0].Rows[0]["pwd"] + "<br><br>";
                Msg.IsBodyHtml = true;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("tmjagadeesh.95@gmail.com", "password");
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                //Msg = null;
                lbltxt.Text = "Your Password Details Sent to your mail";
                // Clear the textbox valuess
                txt_email.Text= "";
            }
            else
            {
                lbltxt.Text = "The Email you entered not exists.";
            }
        }
        catch(Exception ex)
    {
            Console.WriteLine("{0} Exception caught.", ex);
    }

    }
}


What I have tried:

i am geting a detail from forget password page and press submit button mail need to send
Posted
Updated 21-Feb-18 20:16pm
v2
Comments
Suvendu Shekhar Giri 21-Feb-18 7:50am    
Are you sure that you are not getting any error?
Please try again with breakpoint and see if the code block "smtp.Send(Msg);" works fine.
Try adding following line after exception msg i.e. Console.Write.....
Console.Readline();
Richard MacCutchan 21-Feb-18 8:13am    
Well done storing passwords in clear text and sending them to the user the same way. Should ensure plenty of hackers get into your system.
F-ES Sitecore 21-Feb-18 8:56am    
This is a very frequently asked question, google for articles on sending smtp mail through gmail, there are many caveats as to why this might not be working. For a start you need to configure your account to allow mail to be sent programatically, and it might be waiting for a "captcha" response from you also.

Don't use gamil to send mails, use the smtp server provided by your webhost\isp\company.

1 solution

try this one -
working for me

C#
using System.Net.Mail;
using System.Text;

...

// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("user@gmail.com","password");

MailMessage mm = new MailMessage("mymail@domain.com", "sender@domain.com", "test", "test");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

client.Send(mm);
 
Share this answer
 
Comments
Member 13688916 13-Mar-18 4:31am    
ya my code is working fyn seeting need to change in gmail.account,thanks u
RDBurmon 23-Mar-18 2:51am    
perfect, if that worked then accept and vote the answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900