C# .NET - sending account details to gmail - Asked By kiran Kumar on 14-Nov-11 01:18 AM

hi,

  in my windows forms application project, in the form i have placed 3 textbox controls and one button[submit].


   textbox1 ---> username
  
   textbox2 ---> password

   textbox3 ---> emailid  (to send the username and password created )

  if i click on submit button, the username and password details have to be send to that emailid entered in the textbox3 



  how to do this?? 

  
dipa ahuja replied to kiran Kumar on 14-Nov-11 01:25 AM
protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress = TextBox3.Text;
  string GmailId = "abc@gmail.com";
  string password = "";
  string bodyMsg = "UserName=" + TextBox1.Text + "<Br> Password=" + TextBox2.Text;
 
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = txtSubject.Text;
    mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
   
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");
  smtp.EnableSsl = true;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
  smtp.Send(mail);
}
Jitendra Faye replied to kiran Kumar on 14-Nov-11 01:30 AM
You have to use the namespace System.Net.Mail

In the button click write this below code to send email

 
using System;
using System.Windows.Forms;
using System.Net.Mail;
 
 
private void BtnSend_Click(object sender, EventArgs e)
  {
  try
  {
  MailMessage mail = new MailMessage();
  SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
 
  mail.From = new MailAddress("your_email_address@gmail.com");
  mail.To.Add(Txt3.Text);
  mail.Subject = "Test Mail";
  mail.Body = "UserId "+ Txt1.Text + " Password-" + Txt2.Text;
 
  SmtpServer.Port = 587;
  SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
  SmtpServer.EnableSsl = true;
 
  SmtpServer.Send(mail);
  MessageBox.Show("mail Send");
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.ToString());
  }
  }


Try this a nd let me know.
kiran Kumar replied to dipa ahuja on 14-Nov-11 01:38 AM
hi dipa,


  i write the code as u mentioned, while debugging, it is showing the following error:

  The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. 



how to solve this??
dipa ahuja replied to kiran Kumar on 14-Nov-11 02:50 AM
Have you provided your Gmail Username And password on highlighted staetmenet here:


protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress = TextBox3.Text;
  string GmailId = "abc@gmail.com"; //YOUR GMAIL USERNAME
  string password = "";             //YOUR GMAIL PASSWORD 
  string bodyMsg = "UserName=" + TextBox1.Text + "<Br> Password=" + TextBox2.Text;
 
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = txtSubject.Text;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
   
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");
  smtp.EnableSsl = true;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
  smtp.Send(mail);
}

kiran Kumar replied to dipa ahuja on 14-Nov-11 03:32 AM
thank u dipa, its working now! 

  but i gave my email id along with password, what if anyone hack it ?? is it secured ?? if so, can i encrypt it and use it ?
dipa ahuja replied to kiran Kumar on 14-Nov-11 06:15 AM
Your Welcome!

Yes its secure, no one can hack it.

But if you make it more secure then put the account detail inside the web.config 

try this way:

Step 1 : Add this in Web.config
    
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
  port="587"
  userName=""
  password=""
</smtp>
</mailSettings>
</system.net>
 
Step 2 : Mail Sending Code
protected void btnSent_Click(object sender, EventArgs e)
{
   string toEmailAddress = "xx@gmail.com";
   string GmailId = "abc@gmail.com";
   string bodyMsg = "helo its testing mail";
   string subject = "testing mail";
   MailMessage mail = new MailMessage();
   mail.To.Add(toEmailAddress);
  mail.From = new MailAddress(GmailId);
   mail.Subject = subject;
   mail.Body = bodyMsg;
   mail.IsBodyHtml = true;
   SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
  smtp.Send(mail);
}
 
Kirtan Patel replied to kiran Kumar on 14-Nov-11 10:11 PM
Step 1 : Add Namespace
 
using System.Net;
using System.Net.Mail;
 
Step 2 :Write code
 
protected void btnSend_Click(object sender, EventArgs e)
{
  SmtpClient client = new SmtpClient();
  MailMessage message = new MailMessage();
  client.Port = 587;
  client.Host = "smtp.gmail.com";
  client.UseDefaultCredentials = true;
  client.Credentials = new System.Net.NetworkCredential(txtemail.Text ,txtpass.Text );
  client.EnableSsl = true;
  try
  {
    MailAddress SendTo = new MailAddress(txtTo.Text);
    MailAddress SendFrom = new MailAddress(txtFrom.Text);
    message = new MailMessage(SendFrom, SendTo);
    message.Body = txtMessage.Text;
    message.Subject = txtSub.Text;
    client.Send(message);
    txtStatus.Text = "Your email has been sent";
          
  }
  catch (Exception ex)
  {
    txtStatus.Text = ex.ToString();
  }
}