With sending attachment via email via ASP.NET application

1

I already have a method in my controller that sends email. Now you need to see attachment (all attachment types, jpg, pdf, doc ...);

string  strPara = Request.Form["email"];
                string strDe = "[email protected]";
                string strTexto = "teste";

                SmtpClient client = new SmtpClient();
                client.Port = 25;
                client.Host = "exchange.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(strDe, strPara, "teste", strTexto);
                mm.BodyEncoding = UTF8Encoding.UTF8;
                mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

                client.Send(mm);

                return View();
    
asked by anonymous 22.06.2018 / 21:22

1 answer

2

Only you use the System.Net.Mail.Attachment class:

using (SmtpClient smtpCliente = new SmtpClient())
{
     MailMessage mail = new MailMessage();

     smtpCliente.Host = param.Smtp;
     smtpCliente.Port = param.Porta;
     smtpCliente.EnableSsl = param.SSL;
     smtpCliente.UseDefaultCredentials = false;
     smtpCliente.Credentials = new System.Net.NetworkCredential(param.Login, param.Senha);
     smtpCliente.Timeout = 120000;
     mail.Subject = assunto;
     mail.Body = mensagem;
     if (!String.IsNullOrEmpty(anexo)) //anexo é o nome do arquivo
     {
         Attachment anexar = new Attachment(anexo);
         mail.Attachments.Add(anexar);
     }
     mail.From = new MailAddress(param.Login);
     mail.To.Add(new MailAddress(email));
     smtpCliente.Send(mail);
 }
    
22.06.2018 / 21:35