Click here to Skip to main content
15,899,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application,we have to send email to gmail on button click event using smtp protocol.The code given below doesn't work.Please give me solution.
Thank You.

C#
string emailid;
        string Subject = "Request For Registration";
        string MailBody = "Please Approve My Request";
        string strquery3 = "select EmailId From  tblAdmin_Registration Where(FacultyId = '" + facultyid + "')";
        objsqlcommand = new SqlCommand(strquery3, objsqlconnection);

try
			{
				SqlDataAdapter adapter = new SqlDataAdapter(objsqlcommand);
				DataTable table = new DataTable();
				adapter.Fill(table);
				if (table.Rows.Count > 0)
				{
					Server.ScriptTimeout = 1000;
					Response.Flush();
					emailid = table.Rows[0]["EmailId"].ToString();
					MailMessage ObjMail = new MailMessage();
					SmtpClient ObjSmtpServer = new SmtpClient("objSmtpServer.gmail.com");
					ObjMail.From = new MailAddress(txtemail.Text);
					ObjMail.To.Add(emailid);
					ObjMail.Subject = Subject;
					ObjMail.Body = MailBody;
					try
					{
						ObjSmtpServer.Send(ObjMail);
					}
					catch
					{
						Response.Write("Error in sending Mail");
					}
					Response.Flush();
				}

			}
		    catch
				{
					Response.Write("Error111111111111........");

				}
Posted
Updated 22-Aug-11 20:04pm
v2
Comments
walterhevedeich 23-Aug-11 2:04am    
Any error?

try to use this method
C#
public Boolean SendingMail(string From, string To, string Subject,string Body)
	{
        try
        {
            MailMessage m = new MailMessage("Uma Shankar<test1@gmail.com>", To);
            m.Subject = Subject;
            m.Body = Body;
            m.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "mail.google.com";
            NetworkCredential authinfo = new NetworkCredential("test@gmail.com", "123456.com");
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = authinfo;
            smtp.Send(m);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
	}
 
Share this answer
 
v2
 
Share this answer
 
string strmessage = "Name : " + txtName.Text + "\n" + "Email : " + txtEmail.Text + "\n" + "MobileNo : " + txtContactNo.Text + "\n" + " Message : " + txtRemarks.Text + "\n" + "Departing Date : " + txtDeparting.Text + "\n" + " Returning Date : " + txtreturning.Text + "\n" + "No of persons : " + txtNoOfPerson.Text + "\n" + "Leaving From : " + txtleavingFrom.Text + "\n" + "Going To : " + txtGoingTo.Text + "\n";
MailMessage msg = new MailMessage();
msg.To = "drtours@mail.com";
msg.From = "drtours@mail.com";
msg.Subject = "Contact Details";
msg.Body = strmessage;
lblMsg.Visible = true;
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "contact@gmail.com");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "dryrws123");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(msg);



//contact@gmail.com must be a valid gmailid.
 
Share this answer
 
v2

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