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 useHttpPostedFileBase
?– Ricardo Pontual
is that mm.Attachments.Add(new Attachment(file, "Attachment")) expects a string. What it would look like?
– Danielle Arruda torres
The class
HttpPosteFileBase
has the methodInputStream
to resolve this :) :mm.Attachments.Add(new Attachment(arquivo.InputStream, "attachment"))
– Ricardo Pontual
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
– Danielle Arruda torres
Yes that’s right for multiple files, use a List instead of the array, put in the answer
– Ricardo Pontual