When submitting Form the attachment is coming null. ASP.NET

1

When submitting the form, everything in the controller arrives completed, minus the file that is attached. Where is the error?

Controller:

 public ActionResult EnviaEmail(string destinatario, string assunto, string mensagem, FileStream arquivo)
            {
                SmtpClient client = new SmtpClient();
                client.Port = 28;
                client.Host = "exchange.minhaempresa.local";
                client.EnableSsl = true;
                client.Timeout = 10000;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("gustavo.r", "123456");

                MailMessage mm = new MailMessage("[email protected]", destinatario, assunto, mensagem);
                mm.BodyEncoding = UTF8Encoding.UTF8;
                mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

                mm.Attachments.Add(new Attachment(arquivo, "attachment"));
                //mm.Attachments.Add(new Attachment(fileAttachment));

                client.Send(mm);

                return View();
            }

html:

 <form id="formulario" action="~/Gerenciamento/EnvioEmail/EnviaEmail" method="post" enctype="multipart/form-data">
        <label>Destinatario:</label><input type="text" name="destinatario" /><br />
        <label>Assunto:</label><input type="text" name="assunto" /><br />
        <label>Mensagem:</label> <textarea name="mensagem" /></textarea><br />
        <label>Anexo:</label><input name="arquivo" type="file" />
        <button>Enviar</button>
    </form>
    
asked by anonymous 25.06.2018 / 13:58

2 answers

2

Replace class FileStream with HttpPostedFileBase :

public ActionResult EnviaEmail(string destinatario, string assunto, string mensagem, FileStream arquivo)

To read the Stream , use the InputStream method:

mm.Attachments.Add(new Attachment(arquivo.InputStream, "attachment"))

On multiple files, simply change the html to allow multiple files to be selected:

<input name="arquivo" type="file" multiple="multiple"/>

In Action , you now receive a List :

List<HttpPostedFileBase> arquivos

And then a foreach for each file posted:

foreach (HttpPostedFileBase postedFile in arquivos)
{
  ....
}
    
25.06.2018 / 14:56
2

The complete answer works, based on the suggestion of @Ricardo Pontual

 public ActionResult EnviaEmail(string destinatario, string assunto, string mensagem, IEnumerable<HttpPostedFileBase> fileUploader)// string fileAttachment)
        {
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.Host = "exchange.minhaempresa.local";
            client.EnableSsl = true;
            client.Timeout = 40000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("gustavo.r", "123456");

            MailMessage mm = new MailMessage("[email protected]", destinatario, assunto, mensagem);
            mm.BodyEncoding = UTF8Encoding.UTF8;
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            // string fileName = Path.GetFileName(fileUploader.FileName);
            // mm.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));

            string fileName = "";
            if (fileUploader != null)

            {

                foreach (var file in fileUploader)
                {
                       fileName = Path.GetFileName(file.FileName);
                        mm.Attachments.Add(new Attachment(file.InputStream, fileName));

                }

            }



            client.Send(mm);

            return View();
        }

html:

  <form id="formulario" action="~/Gerenciamento/EnvioEmail/EnviaEmail" method="post" enctype="multipart/form-data">
        <label>Destinatario:</label><input type="text" name="destinatario" /><br />
        <label>Assunto:</label><input type="text" name="assunto" /><br />
        <label>Mensagem:</label> <textarea name="mensagem" /></textarea><br />
        <label>Anexo:</label><input name="fileUploader" type="file" multiple />
        <button>Enviar</button>
    </form>
    
25.06.2018 / 15:07