예제 #1
0
    private bool Validar_Email(string strTexto)
    {
        char[] chSeparador = new char[] { ' ' };
        if (strTexto.Contains(","))
        {
            chSeparador[0] = ',';
        }
        else
        if (strTexto.Contains(";"))
        {
            chSeparador[0] = ';';
        }

        string[] strEmails = strTexto.Split(chSeparador);

        int intValidos = 0;

        foreach (string strEmail in strEmails)
        {
            if (!string.IsNullOrEmpty(strEmail))
            {
                if (!CRutinas.Validar_Email(strEmail))
                {
                    return(false);
                }
                else
                {
                    intValidos++;
                }
            }
        }

        if (intValidos > 0)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
예제 #2
0
    public bool Enviar(out string strError)
    {
        strError = string.Empty;

        if (string.IsNullOrEmpty(strAsunto))
        {
            strError = "Asunto es requerido";
            return(false);
        }

        if (string.IsNullOrEmpty(strDe))
        {
            strError = "Dirección de correo DE es requerida";
            return(false);
        }

        if (!CRutinas.Validar_Email(strDe))
        {
            strError = "Dirección de correo DE no es válida";
            return(false);
        }

        if (strPara.Count == 0)
        {
            strError = "Dirección de correo PARA es requerida";
            return(false);
        }

        foreach (string strEmail in strPara)
        {
            if (!CRutinas.Validar_Email(strEmail))
            {
                strError = "Dirección de correo PARA no es válida";
                return(false);
            }
        }

        if (strPara.Count != 0)
        {
            foreach (string strEmail in strCC)
            {
                if (!CRutinas.Validar_Email(strEmail))
                {
                    strError = "Dirección de correo CC no es válida";
                    return(false);
                }
            }
        }

        HttpCookie ckSIAN   = System.Web.HttpContext.Current.Request.Cookies["userCng"];
        string     img_path = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + System.Web.HttpContext.Current.Request.ApplicationPath + ckSIAN["ck_logo"];

        string[] strSize       = ckSIAN["ck_logo_size"].Split('x');
        int      intLogoWidth  = int.Parse(strSize[0]);
        int      intLogoHeight = int.Parse(strSize[1]);
        int      max_pixeles   = 200;

        if (intLogoWidth > max_pixeles || intLogoHeight > max_pixeles)
        {
            double relacion = 0;
            if (intLogoWidth > intLogoHeight)
            {
                relacion = max_pixeles / (double)intLogoWidth;
            }
            else
            {
                relacion = max_pixeles / (double)intLogoHeight;
            }

            intLogoWidth  = (int)(intLogoWidth * relacion);
            intLogoHeight = (int)(intLogoHeight * relacion);
        }

        string strImgSize = " height=\"" + intLogoHeight.ToString() +
                            "\" width=\"" + intLogoWidth.ToString() + "\"";

        MailMessage objMailMensaje = new MailMessage();

        objMailMensaje.From = new MailAddress(this.strDe);

        foreach (string strEmail in strPara)
        {
            objMailMensaje.To.Add(new MailAddress(strEmail));
        }

        foreach (string strEmail in strCC)
        {
            objMailMensaje.CC.Add(new MailAddress(strEmail));
        }

        objMailMensaje.Subject = strAsunto.Trim();

        LinkedResource logo = new LinkedResource(img_path);

        logo.ContentId = "companylogo";
        AlternateView av1 = AlternateView.CreateAlternateViewFromString(
            "<html><body><table><tr><td style='font-size: 11px; font-family: Verdana;'>" +
            strMensaje.Trim().Replace("\r\n", "<br />") +
            "</td></tr><tr>" +
            "<td align='left'><img src=cid:companylogo" + strImgSize + "/></td>" +
            "</tr></table></body></html>",
            null, System.Net.Mime.MediaTypeNames.Text.Html);

        av1.LinkedResources.Add(logo);

        objMailMensaje.AlternateViews.Add(av1);
        objMailMensaje.IsBodyHtml = true;

        foreach (string strAtt in strAttachments)
        {
            try
            {
                if (File.Exists(strAtt))
                {
                    objMailMensaje.Attachments.Add(new Attachment(strAtt));
                }
            }
            catch
            { }
        }

        SmtpClient objSmtp = new SmtpClient();

        try
        {
            objSmtp.Send(objMailMensaje);
            return(true);
        }
        catch (Exception ex)
        {
            strError = "No se pudo enviar el correo: " + ex.Message;
        }

        return(false);
    }