-1

I got this idea of making a mail sender. I tried to look for solutions but none really worked and they were badly explained or if it is even possible to do this?
So i basically have this if else code that check's if it's empty or not and if it's not it would send the value to mail.

using System.Net.Mail; //i think this is what i need?

private void button1_Click(object sender, EventArgs e)
{
    if(string.IsNullOrWhiteSpace(textBox1.Text) && string.IsNullOrWhiteSpace(textBox2.Text))
    {
        MessageBox.Show("You're empty!");
    }
    else if(Int32.Parse(textBox1.Text) != 0)
    {
        // send text box to mail
    }
    else
    {
        MessageBox.Show("Something went wrong.");
        System.Threading.Thread.Sleep(2000);
        MessageBox.Show("Closing");
        System.Threading.Thread.Sleep(1000);
        this.Close();
    }
}

Is someone willing to direct me in the correct direction or perhaps help me explain how to do it?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120

2 Answers2

2

You can put textBox1.Text as an email body, something like this :

mail.From = new MailAddress(emailaddress);
mail.To.Add(recipient);
mail.Subject = "Test Mail";

mail.Body = textBox1.Text; // sets the body to the text box's content

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(emailaddress, password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

System.Windows.Forms.MessageBox.Show("Mail sent");
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Edin P
  • 36
  • 3
1
Try this :
private void button1_Click(object sender, EventArgs e)
{
sendMailToAdmin(textbox1.Text,textbox2.text);
}

protected void sendMailToAdmin(string uname, string email)
{
    MailMessage myMsg = new MailMessage();
    myMsg.From = new MailAddress("****@mail.com");
    myMsg.To.Add(email);
    myMsg.Subject = "New User Email ";
    myMsg.Body = "New User Information\n\nUser Name: " + uname + "\nEmail : " + email;

    // your remote SMTP server IP.
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials = new System.Net.NetworkCredential("****@mail.com", "pass***");
    smtp.EnableSsl = true;
    smtp.Send(myMsg);
}
Mandar Dhadve
  • 371
  • 2
  • 12
  • Can you explain the purpose of the port 587? – Insanebench420 May 25 '18 at 14:34
  • Port 587 is an MSA (message submission agent) port that requires SMTP authentication & is often used instead of port 25 in situations where the ISP blocks port 25. More information can be found here: http://help.altn.com/mdaemon/en/default-domain-and-servers_ports.htm?zoom_highlightsub=ports – Mandar Dhadve May 28 '18 at 04:26