How do I send an email without sending an attachment?

Asked

Viewed 691 times

2

Hello, I have a question: I made a script to send email, but I would like that when sending it the user does not necessarily need to put an attachment (ie leave the sending of the attachment not mandatory). How could I do that?

Follows the script:

public void EnviarEmail()
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        //Identificação do destinario
        msg.To.Add(txtPara.Text);
        //Identificação do Email
        msg.From = new System.Net.Mail.MailAddress("[email protected]");
        //assunto do email
        msg.Subject = txtAssunto.Text;
        //Corpo do email
        msg.Body = txtMensagem.Text;
        //Anexo
        System.Net.Mail.Attachment anexo = new System.Net.Mail.Attachment(txtAnexo.Text);
        msg.Attachments.Add(anexo);
        //coloque seu servidor smtp e porta 587 (configuração smtp do gmail)
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.live.com", 587);
        smtp.EnableSsl = true;

        //login do email que vai enviar as mensagens
        smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "senha");
        smtp.Send(msg);
        MessageBox.Show("Email enviado!");
        txtAnexo.Clear();
        txtAssunto.Clear();
        txtMensagem.Clear();
        txtPara.Clear();     
    }

private void btnEnviar_Click(object sender, EventArgs e)
    {
            EnviarEmail();    
    }

That’s it. Could someone help me? Thank you.

2 answers

2

Just remove the lines you create and add an attachment to the message. There is nothing that requires an attachment to be added.

msg.Body = txtMensagem.Text;

//Anexo
//System.Net.Mail.Attachment anexo = new System.Net.Mail.Attachment(txtAnexo.Text);
//msg.Attachments.Add(anexo);

If you want to make attachment submission optional, just use one condition

if(anexoValido)
{
    System.Net.Mail.Attachment anexo = new System.Net.Mail.Attachment(txtAnexo.Text);
    msg.Attachments.Add(anexo);
}

1


You can do it like this:

This code does a check to see if any attachments have been informed, and it checks if the file actually exists so that it can be sent, if these conditions are not true it sends the email without attachment.

if (!string.IsNullOrEmpty(txtAnexo.Text) && File.Exists(txtAnexo.Text))
{
    System.Net.Mail.Attachment anexo = new System.Net.Mail.Attachment(txtAnexo.Text);
    msg.Attachments.Add(anexo);
}

I noticed that you are not using using in your code, it serves to free the memory objects when they are no longer being used.

Here explains the importance of using: What is the usefulness of using?

    public void EnviarEmail()
    {
        using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
        {
            //Identificação do destinario
            msg.To.Add(txtPara.Text);

            //Identificação do Email
            msg.From = new System.Net.Mail.MailAddress("[email protected]");

            //assunto do email
            msg.Subject = txtAssunto.Text;

            //Corpo do email
            msg.Body = txtMensagem.Text;

            //Anexo - 
            //Verifica se a txtAnexo.Text não é vazio or null e verifica se existe o anexo no caminho informado
            // Se tiver tudo ok ele envia com anexo, se não envia sem anexo
            if (!string.IsNullOrEmpty(txtAnexo.Text) && File.Exists(txtAnexo.Text))
            {
                System.Net.Mail.Attachment anexo = new System.Net.Mail.Attachment(txtAnexo.Text);
                msg.Attachments.Add(anexo);
            }

            //coloque seu servidor smtp e porta 587 (configuração smtp do gmail)
            using (System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.live.com", 587))
            {
                smtp.EnableSsl = true;

                //login do email que vai enviar as mensagens
                smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "senha");
                smtp.Send(msg);

                MessageBox.Show("Email enviado!");

                txtAnexo.Clear();
                txtAssunto.Clear();
                txtMensagem.Clear();
                txtPara.Clear();
            }
        }
    }
  • Here gave error in the file saying it is not part of the parameter

  • I did the test here by passing "C: Meuanexo.PNG" in txtAnexo.Text and I put this file in my C directory. You can also call a Validatedvalidated method(txtAnexo.Text) by doing its logic to validate whether the attachment should be sent or not.

  • Thank you very much, I managed to settle here! :)

Browser other questions tagged

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