1

i am creating an simple email sending application, in my application every time i have to put email id or password but i want to save them one i have put it in text fields like we save our username or passwords for login.

this is my code:

        try
        {
            // setup mail message
            MailMessage message = new MailMessage();
            message.From = new MailAddress(textBox1.Text);
            message.To.Add(new MailAddress(textBox2.Text));
            message.Subject = textBox3.Text;
            message.Body = richTextBox1.Text;

            // setup mail client
            if (textBox1.Text.Contains("gmail"))
            {
                SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
                mailClient.Credentials = new NetworkCredential(textBox1.Text, textBox4.Text);
                mailClient.Send(message);

                MessageBox.Show("Sent from Gmail");
            }
            else if (textBox1.Text.Contains("yahoo"))
            {
                SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com");
                mailClient.Credentials = new NetworkCredential(textBox1.Text, textBox4.Text);
                mailClient.Send(message);

                MessageBox.Show("Sent from Yahoo");
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Error");
        }
Jonas Gobel
  • 153
  • 3
  • 5
  • 13
  • Is this winforms, webforms or WPF? – Jordy van Eijk Sep 19 '12 at 08:48
  • c# Windows Form Application .Net 2.0 – Jonas Gobel Sep 19 '12 at 08:49
  • 1
    Just for convenience I would advise you to give text boxes a meaningful name rather then 1,2,3. You can consider using [SecureString](http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx) (and then use it later, [this](http://stackoverflow.com/questions/1800695/c-sharp-securestring-question) might help) if I understand what you are trying to do. – Scis Sep 19 '12 at 08:51
  • what i am trying is simple i created a email sending application and i want that when once i put my user id or password in textfields like username or password it will save that entry just like we save save our username or passwords for facebook or etc... – Jonas Gobel Sep 19 '12 at 08:56

1 Answers1

1

If it is just for one user. You can put this information in the App.Config if it will be more users you can set this an a datasource (Database, XML, etc.)

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
  • will you please tell me how can i save username or password in XML because i am beginner in c#/.net programing – Jonas Gobel Sep 19 '12 at 09:02
  • Hi Jonas, See [here](http://csharp.net-informations.com/xml/how-to-create-xml.htm) a simple tutorial about how to create XML file from C# – Jordy van Eijk Sep 19 '12 at 09:12
  • You also might want to encrypt the password in App.Config, instead of storing as plain text. You can use following code http://codereview.stackexchange.com/questions/15346/using-system-security-cryptography-protecteddata-do-you-see-any-issue-with-encr – Ankush Sep 19 '12 at 15:22
  • Hello Mr. Jordy van Eijk, i have created xml file successfully and i have saved my textboxes username or password entry in it but i want that when i click in my textbox it should show all the saved username or password in textbox just like when we save our login username or password and it shows us all our saved username or passwords in it. and when i click on it it should display in textbox (username or password field) – Jonas Gobel Sep 20 '12 at 03:37
  • And i also want that if i put the same username or password that i have saved before it will replace it or should not give it a new place – Jonas Gobel Sep 20 '12 at 03:40
  • this is the code: `XmlTextWriter writer = new XmlTextWriter("entry.xml", System.Text.Encoding.UTF8); writer.WriteStartDocument(true); writer.Formatting = Formatting.Indented; writer.Indentation = 2; writer.WriteStartElement("Table"); createNode(textBox2.Text, textBox1.Text, textBox4.Text, writer); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); MessageBox.Show("XML File created ! ");` – Jonas Gobel Sep 20 '12 at 03:56
  • `private void createNode(string pID, string pName, string pPrice, XmlTextWriter writer) { writer.WriteStartElement("Entry"); writer.WriteStartElement("To"); writer.WriteString(pID); writer.WriteEndElement(); writer.WriteStartElement("From"); writer.WriteString(pName); writer.WriteEndElement(); writer.WriteStartElement("Password"); writer.WriteString(pPrice); writer.WriteEndElement(); writer.WriteEndElement(); }` – Jonas Gobel Sep 20 '12 at 03:57