Exemplo n.º 1
0
        /// <summary>
        /// Envio de email con varios adjuntos.
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isHtmlBody"></param>
        /// <param name="attachments"></param>
        /// <param name="resultMessage"></param>
        /// <returns></returns>
        public static bool SendMailWithAttachment(string to, string subject,
                                                  string body,
                                                  bool isHtmlBody,
                                                  IList <Adjunto> attachments,
                                                  out string resultMessage)
        {
            //SE VALIDA CONFIGURACION DE CORREO
            if (string.IsNullOrEmpty(ISConfiguration.GetConfigurationParameter("Email", "_appMail_FromAddress")))
            {
                resultMessage = "Invalid configuration value: FromAddress";
                ISException.RegisterExcepcion(resultMessage);
                return(false);
            }

            try
            {
                using (var mailMessage = new MailMessage
                {
                    From = new MailAddress(ISConfiguration.GetConfigurationParameter("Email", "_appMail_FromAddress")),
                    Subject = subject,
                    Body = body,
                    IsBodyHtml = isHtmlBody,
                })
                {
                    mailMessage.To.Add(to);
                    //SE AGREGAN LOS ADJUNTOS
                    if (attachments != null && attachments.Count > 0)
                    {
                        for (int i = 0; i < attachments.Count; i++)
                        {
                            if (!string.IsNullOrEmpty(attachments[i].Contenido) && !string.IsNullOrEmpty(attachments[i].Nombre))
                            {
                                //SE CARGA EL ADJUNTO AL MENSAJE
                                byte[] filebytes = Convert.FromBase64String(attachments[i].Contenido);
                                Stream stream    = new MemoryStream(filebytes);
                                mailMessage.Attachments.Add(new Attachment(stream, attachments[i].Nombre));
                            }
                        }
                    }

                    //SE CREA EL CLIENTE SMTP
                    var smtpClient = new SmtpClient
                    {
                        Host =
                            ISConfiguration.GetConfigurationParameter("Email", "_appMail_SmtpServer"),
                        Port =
                            ISConvert.ToInteger(ISConfiguration.GetConfigurationParameter("Email",
                                                                                          "_appMail_SmtpPort")),
                        Credentials =
                            new NetworkCredential(
                                ISConfiguration.GetConfigurationParameter("Email", "_appMail_User"),
                                ISConfiguration.GetConfigurationParameter("Email", "_appMail_Password"),
                                ISConfiguration.GetConfigurationParameter("Email", "_appMail_Domain")),
                        EnableSsl =
                            ISConvert.ToBoolean(ISConfiguration.GetConfigurationParameter("Email",
                                                                                          "_appMail_AuthenticationRequired"))
                    };

                    //SE ENVIA EL MAIL
                    //smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtpClient.Send(mailMessage);
                }
                resultMessage = string.Empty;
                return(true);
            }
            catch (FormatException ex)
            {
                resultMessage = ex.Message;
                ISException.RegisterExcepcion(ex);
                return(false);
            }
            catch (SmtpException ex)
            {
                resultMessage = ex.Message;
                ISException.RegisterExcepcion(ex);
                return(false);
            }
            catch (Exception ex)
            {
                resultMessage = ex.Message;
                ISException.RegisterExcepcion(ex);
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool SendMail(string[] strListTo, string[] strListCc, string strSubject, string strBody,
                             bool blnIsBodyHtml, bool blnAsyncMethod, object objState, out string _strResultMessage)
        {
            if (string.IsNullOrEmpty(ISConfiguration.GetConfigurationParameter("Email", "_appMail_FromAddress")))
            {
                _strResultMessage = "Invalid configuration value: FromAddress";
                ISException.RegisterExcepcion(_strResultMessage);
                return(false);
            }

            try
            {
                var objMailMessage = new MailMessage();

                objMailMessage.From =
                    new MailAddress(ISConfiguration.GetConfigurationParameter("Email", "_appMail_FromAddress"));

                for (int index = 0; index < strListTo.Length; index++)
                {
                    objMailMessage.To.Add(strListTo[index]);
                }

                for (int index = 0; index < strListCc.Length; index++)
                {
                    objMailMessage.CC.Add(strListCc[index]);
                }

                objMailMessage.Subject    = strSubject;
                objMailMessage.Body       = strBody;
                objMailMessage.IsBodyHtml = blnIsBodyHtml;

                var objSmtpClient = new SmtpClient();
                objSmtpClient.Host = ISConfiguration.GetConfigurationParameter("Email", "_appMail_SmtpServer");
                objSmtpClient.Port =
                    ISConvert.ToInteger(ISConfiguration.GetConfigurationParameter("Email", "_appMail_SmtpPort"));
                ;
                objSmtpClient.Credentials =
                    new NetworkCredential(ISConfiguration.GetConfigurationParameter("Email", "_appMail_User"),
                                          ISConfiguration.GetConfigurationParameter("Email", "_appMail_Password"),
                                          ISConfiguration.GetConfigurationParameter("Email", "_appMail_Domain"));
                objSmtpClient.EnableSsl =
                    ISConvert.ToBoolean(ISConfiguration.GetConfigurationParameter("Email",
                                                                                  "_appMail_AuthenticationRequired"));

                if (blnAsyncMethod)
                {
                    objSmtpClient.SendCompleted += objSmtpClient_SendCompleted;
                    objSmtpClient.SendAsync(objMailMessage, objState);

                    _strResultMessage = string.Empty;
                    return(true);
                }

                objSmtpClient.Send(objMailMessage);
                _strResultMessage = string.Empty;
                return(true);
            }
            catch (FormatException ex)
            {
                _strResultMessage = ex.Message;
                ISException.RegisterExcepcion(ex);
                return(false);
            }
            catch (SmtpException ex)
            {
                _strResultMessage = ex.Message;
                ISException.RegisterExcepcion(ex);
                return(false);
            }
            catch (Exception ex)
            {
                _strResultMessage = ex.Message;
                ISException.RegisterExcepcion(ex);
                return(false);
            }
        }