-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?

5
  • Why are you doing an integer parse on the textbox?? Commented May 25, 2018 at 11:48
  • So that i can also send numbers? Commented May 25, 2018 at 11:49
  • stackoverflow.com/questions/9201239/… Commented May 25, 2018 at 11:52
  • You don't need to parse it, you can send numbers as a string value. Commented May 25, 2018 at 11:52
  • 1
    .NET has a pretty detailed documentation, I would suggest you to read the examples there: learn.microsoft.com/en-us/dotnet/api/… Commented May 25, 2018 at 11:54

2 Answers 2

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"); 
Sign up to request clarification or add additional context in comments.

Comments

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); } 

2 Comments

Can you explain the purpose of the port 587?
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: help.altn.com/mdaemon/en/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.