简体   繁体   中英

How do I get information from a text box sent to my email? (C#, Visual studio 2013)

Yes I know it's very broad but I am relativity new to programming in C#. In my system there are a couple of text boxes and I want the information to be sent to my email when the user presses the button on my program.

Text box names:

textbox1
textbox2

Button name:

btnsend

Code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsFormsApplication1
{
    public partial class form1 : Form
    {
        public form1()
        {
            InitializeComponent();
        }
        button1.Click += button1_Click;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage("myemail", "myemail2");
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("myemail");
                mail.To.Add("myemail2");
                mail.Subject = textBox1.Text;
                mail.Body = textBox2.Text;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("myemail","myemailpassword");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("Email sent");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
    }
}

The code is now trying to do what it is intended to do. I now get this error when I press button1: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated The server response was 5.5.1 Authentication required.

To send emails through c#, you need some more practices- here's are some link-

  1. Send Email in c#
  2. C#.Net How To: Send email from gmail using c#
  3. Send Email via SMTP(Stack Overflow)

and Here's the excerpt from the post-

Now only you have to do is to, get the values from your textbox and put it as you relates. Assuming, textbox for Subject is textbox1, and for body is textbox2.

You can write the code like-

// Attach the btnSend click event where it is created or before it suites better in you code.
btnsend.Click += btnsend_Click;

        void btnsend_Click(object sender, RoutedEventArgs e)
        {
            MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = "smtp.google.com";
            mail.To.Add("reciever@example.com");
            mail.Subject = textbox1.Text;
            mail.Body = textbox2.Text;
            client.Send(mail);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM