The problem is here:
Attachment data = new Attachment(txtattachment.Text);
if (txtattachment.Text == "")
{
msg.Attachments.Add(data);
client.Send(msg);
}
else
{
msg.Attachments.Add(data);
client.Send(msg);
}
You're creating an
Attachmment
even if the user hasn't selected a file, which will throw an
ArgumentNullException
. You then have an
if..else
block to test whether the user has selected a file, but the content of both branches is identical.
Try this:
if (!string.IsNullOrEmpty(txtattachment.Text))
{
Attachment data = new Attachment(txtattachment.Text);
msg.Attachments.Add(data);
}
client.Send(msg);