By sending Form the Attachment is coming null. ASP.NET

Asked

Viewed 43 times

0

When you submit the form, everything in the controller arrives filled, except 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>
  • I never uploaded using the class FileStream, tried to use HttpPostedFileBase?

  • is that mm.Attachments.Add(new Attachment(file, "Attachment")) expects a string. What it would look like?

  • The class HttpPosteFileBase has the method InputStream to resolve this :) : mm.Attachments.Add(new Attachment(arquivo.InputStream, "attachment"))

  • It worked. Put it in the answer I mark. Something else... Do you have me take an array of files? like how would you like to check the attachments? ex:Httppostedfilebase[] fileUploader

  • Yes that’s right for multiple files, use a List instead of the array, put in the answer

2 answers

1


Replace the class FileStream for HttpPostedFileBase:

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

To read the Stream, use the method InputStream:

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

On multiple files, simply change the html to allow selecting multiple files:

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

In the Action, now receive a List:

List<HttpPostedFileBase> arquivos

And then a foreach for each file posted:

foreach (HttpPostedFileBase postedFile in arquivos)
{
  ....
}

1

Follow the full answer working, based on @Ricardo Pontual’s suggestion

 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>

Browser other questions tagged

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