Do not always use the boot attached

Asked

Viewed 96 times

-1

inserir a descrição da imagem aquihave a doubt my program consists of sending email with attachments but sometimes when I don’t want to send an email with attachments gives me an error O que faço para não ter que inserir sempre anexos

Anyone can help?

I can send email with attachments without problems but no attachments does 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;
    }
}

Additional information error: The filename parameter cannot be an empty string.

  • 1

    From the picture it’s obvious that Anexostxt.Text is empty, why? Just seeing the code. Show the code snippet where Anexostxt.Text is set.

  • this print helps?

  • No, from what I understand, Anexostxt.Text refers to the attached file. So where are you setting this method? Something like this: Anexostxt.Text = "c:\meuanexo.txt"

  • 1

    Prefer to put code and not images. Put the exception message and the stack trace of the same.

  • the message is : "The filename parameter cannot be an empty string. r nName of parameter: filename"}

1 answer

0

Assuming that the button4 whatever sends the email, if you do not want attachments you need to check if your attachment field is empty. 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 that the fields do not contain only spaces, or point to a valid file, etc.)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.