I would like some help with my coding please. I am trying to send an message to an email but I keep getting an error after I try to click on the button and send the message. Below is the coding:
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;
using System.Net.Mail;
namespace CO6009DissertationV5
{
public partial class frmSendMsg : Form
{
public frmSendMsg()
{
InitializeComponent();
}
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
System.Net.NetworkCredential userpassword = new System.Net.NetworkCredential();
userpassword.UserName = "user@gmail.com";
userpassword.Password = "password";
client.Credentials = userpassword;
MailMessage msg = new MailMessage("user@gmail.com", "user@gmail.com");
**msg.To.Add(new MailAddress(txtBoxToEmail.Text));**
msg.Body = "<b> Sender's Name: </b>" + txtBoxName.Text + "<p><b> Sender's E-mail: </b>" + txtBoxEmail.Text + "<p><b>Sender's Subject: </b>" + txtBoxSubject.Text + "<p><b>Sender's Message: </b>" + "<p>" + txtBoxMsg.Text;
msg.Subject = txtBoxSubject.Text;
msg.IsBodyHtml = true;
try
{
client.Send(msg);
lblMsgStatus.Text = "<p> Message Successfully Sent </p>";
}
catch (Exception ex)
{
lblMsgStatus.Text = "Send failed";
}
}
}
}
The main problem seems to be this line:
msg.To.Add(new MailAddress(txtBoxToEmail.Text));
As the project keeps stopping here, giving the error line:
{"The parameter 'addresses' cannot be an empty string.\r\nParameter name: addresses"}.
Any help would be appreciated.