With email attachment via ASP.NET application

Asked

Viewed 391 times

0

I already have a method in my controller that sends email. Now you need to contemplate attachments (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();

1 answer

1


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

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);
 }

Browser other questions tagged

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