Do not always use the attached button

-1

IhaveaquestionmyprogramconsistsofsendingemailwithattachmentsbutsometimeswhenIdonotwanttosendanemailwithattachmentsgivemeanerror

Can anyone help?

I can send email with attachments without problems but no attachments do not work

code:

 private void button4_Click(object sender, EventArgs e){
SmtpClient cliente = new SmtpClient();
            MailMessage msg = new MailMessage();

        msg.Attachments.Add(new Attachment(Anexostxt.Text));
        msg.Attachments.Add(new Attachment(anexos2.Text));
    }

private void button6_Click(object sender, EventArgs e){
    OpenFileDialog dlg = new OpenFileDialog();

    if(dlg.ShowDialog()==DialogResult.OK){
        string picpath = dlg.FileName.ToString();
        Anexostxt.Text = picpath;
    }
}

Error Additional information: The parameter 'fileName' can not be an empty string.

    
asked by anonymous 16.05.2015 / 16:29

1 answer

0

Assuming button4 is what sends the email, if you do not want attachments you need to check if your attachment field is blank. For example:

private void button4_Click(object sender, EventArgs e){
    if(Anexostxt.Text != "") {
        msg.Attachments.Add(new Attachment(Anexostxt.Text));
    }
    if(anexos2.Text != "") {
        msg.Attachments.Add(new Attachment(anexos2.Text));
    }
}

This is just a basic principle, you will probably need additional validations (for example, check if the fields do not only contain spaces, or point to a valid file, etc.)

    
16.05.2015 / 17:17