Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I got some code for sending an email for gmail account but it is not working properly
It shows "execution time out period" when I execute that application.

I mention the gmail port number as 465
suppose i change port number it shows "failure to sending" message

I attach my code please tell me solution for these.
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Data.SqlClient;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection("Data Source=ARUN-PC\\SQL;Integrated Security=true;Initial Catalog=TEST"))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT UserName,Password FROM User_Information Where Email= '" + txtEmail.Text.Trim() + "'", con);
                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(txtEmail.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtEmail.Text);
                Msg.Subject = "Your Password Details";
                Msg.Body = "Hi, <br />Please check your Login Detailss<br /><br />Your Username: " + ds.Tables[0].Rows[0]["UserName"] + "<br /><br />Your Password: " + ds.Tables[0].Rows[0]["Password"] + "<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("MyGMAIL@gmail.com", "MYPASSWORD");
                smtp.EnableSsl = true;
                
                smtp.Send(Msg);
                //Msg = null;
                lbltxt.Text = "Your Password Details Sent to your mail";
                // Clear the textbox valuess
                txtEmail.Text = "";
            }
            else
            {
                lbltxt.Text = "The Email you entered not exists.";
            }
        }
        

        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
    }
}

Thanks for all

[Edit]Code block added[/Edit]
Posted
Updated 7-Mar-13 20:34pm
v2
Comments
Michael Haephrati 8-Mar-13 4:36am    
The port number in your example is 587. I suggest that you isolate the functionality form the UI. Just create a function named SendEmail which will not depend on any UI elements such as (lbltxt), so when you publish it, it will be easier for us to test.
giri001 8-Mar-13 4:43am    
i think problem is not in sending mail,because its working fine.you need to debug it carefully.:-)

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