Пример #1
0
 void SaveFile(OpenPop.Mime.Message message)
 {
     foreach (var attachment in message.FindAllAttachments())
     {
         string       filePath     = Path.Combine(@"C:\Attachment", attachment.FileName);
         FileStream   Stream       = new FileStream(filePath, FileMode.Create);
         BinaryWriter BinaryStream = new BinaryWriter(Stream);
         BinaryStream.Write(attachment.Body);
         BinaryStream.Close();
     }
 }
Пример #2
0
        protected void PrepararCorreo()
        {
            try
            {
                using (CobranzasDataContext db = new CobranzasDataContext())
                {
                    idOperador.Value = Request["idOperador"];//Sesion.idOperador.ToString();
                    int id   = Convert.ToInt32(Request["idCorreo"]);
                    int Tipo = Convert.ToInt32(Request["Tipo"]);
                    idCorreo.Value = id.ToString();
                    Entidades.Correos Correo = db.Correos.Single(x => x.idCorreo == id);
                    try
                    {
                        List <Entidades.Correos> CorreoAnterior;
                        List <Entidades.Correos> CorreoSiguiente;
                        if (Tipo == 1)
                        {
                            CorreoSiguiente = db.Correos.Where(x => x.idOperador == Correo.idOperador && x.FechaCreacion < Correo.FechaCreacion && x.TipoEspecial == null && !x.Correos_Personas.Any() && x.RutaEml != null).OrderByDescending(x => x.FechaCreacion).Take(1).ToList();
                            CorreoAnterior  = db.Correos.Where(x => x.idOperador == Correo.idOperador && x.FechaCreacion > Correo.FechaCreacion && x.TipoEspecial == null && !x.Correos_Personas.Any() && x.RutaEml != null).OrderBy(x => x.FechaCreacion).Take(1).ToList();
                        }
                        else if (Tipo == 2)
                        {
                            CorreoSiguiente = db.Correos.Where(x => x.idOperador == Correo.idOperador && x.FechaCreacion < Correo.FechaCreacion && x.Correos_Personas.Any() && x.RutaEml != null && !x.Leido).OrderByDescending(x => x.FechaCreacion).Take(1).ToList();
                            CorreoAnterior  = db.Correos.Where(x => x.idOperador == Correo.idOperador && x.FechaCreacion > Correo.FechaCreacion && x.Correos_Personas.Any() && x.RutaEml != null && !x.Leido).OrderBy(x => x.FechaCreacion).Take(1).ToList();
                        }
                        else
                        {
                            CorreoSiguiente = new List <Entidades.Correos>();
                            CorreoAnterior  = new List <Entidades.Correos>();
                        }

                        if (CorreoSiguiente.Count == 0)
                        {
                            lnkSiguiente.Visible = false;
                        }
                        else
                        {
                            lnkSiguiente.HRef = "ctrlCorreo.aspx?idCorreo=" + CorreoSiguiente.First().idCorreo + "&idOperador=" + idOperador.Value + "&Tipo=" + Tipo;
                        }
                        if (CorreoAnterior.Count == 0)
                        {
                            lnkAnterior.Visible = false;
                        }
                        else
                        {
                            lnkAnterior.HRef = "ctrlCorreo.aspx?idCorreo=" + CorreoAnterior.First().idCorreo + "&idOperador=" + idOperador.Value + "&Tipo=" + Tipo;
                        }
                    }
                    catch { }
                    Correo.Leido = true;
                    db.SubmitChanges();
                    lblAsunto.Text                   = Correo.Asunto;
                    lblDestinatarios.Text            = Server.HtmlEncode(Correo.Destinatarios);
                    lblDestinatariosCopia.Text       = Server.HtmlEncode(Correo.DestinatariosCopia);
                    lblDestinatariosCopiaOculta.Text = Server.HtmlEncode(Correo.DestinatariosCopiaOculta);
                    lblFecha.Text = Correo.FechaCreacion.ToString("dd/MM/yyyy HH:mm:ss tt");
                    String Mensaje = "";
                    //Boolean isHTML = true;
                    if (Correo.Mensaje != null)
                    {
                        Mensaje = Correo.Mensaje;
                    }
                    if (Correo.RutaEml != null)
                    {
                        List <String>            NoAdjuntos = new List <string>();
                        String                   Ruta       = db.Parametros.Single(x => x.Clave == "RutaCorreos").Valor;
                        OpenPop.Mime.Message     m          = OpenPop.Mime.Message.Load(new System.IO.FileInfo(Ruta + Correo.RutaEml + ".eml"));
                        OpenPop.Mime.MessagePart Parte      = m.FindFirstHtmlVersion();
                        if (Parte == null)
                        {
                            Parte = m.FindFirstPlainTextVersion();
                        }
                        Mensaje = Parte.GetBodyAsText();
                        //isHTML = !m.MessagePart.IsText;
                        if (Parte.ContentType.MediaType.ToLower() == "text/plain")
                        {
                            Mensaje = "<pre>" + Mensaje + "</pre>";
                        }
                        try
                        {
                            foreach (Match Match in Regex.Matches(Mensaje, "\"(cid:(\\S+))\""))
                            {
                                //Debug.Print(Match.ToString());
                                String ID = Match.Groups[2].Value;
                                NoAdjuntos.Add(ID);
                                try
                                {
                                    OpenPop.Mime.MessagePart Parte2 = BuscarParte(ID, m.MessagePart);
                                    String Ext = Parte2.FileName;
                                    try
                                    {
                                        Ext = Ext.Substring(Ext.LastIndexOf("."));
                                    }
                                    catch
                                    {
                                        Ext = "";
                                    }
                                    String Ruta2 = Guardar(Parte2, id.ToString());
                                    //Parte2.Save(new FileInfo(MapPath(Ruta2)));
                                    Mensaje = Mensaje.Replace(Match.Groups[0].Value, "\"" + Ruta2 + "\"");
                                }
                                catch { }
                            }
                            foreach (OpenPop.Mime.MessagePart Adjunto in m.FindAllAttachments())
                            {
                                if (NoAdjuntos.Contains(Adjunto.ContentId))
                                {
                                    continue;
                                }
                                String Ruta2 = Guardar(Adjunto, id.ToString());
                                lblAdjuntos.Text += String.Format(@"</span><a href=""{0}"" title=""{2}"" target=""_blank"" class=""AdjuntoSL"">{1}</a><span>", Ruta2, Adjunto.ContentDisposition.FileName ?? "Adjunto", Adjunto.ContentDescription);
                            }
                        }
                        catch { }
                    }
                    //lblAdjuntos.Text=@"</span><a href=""http://www.google.com"" target=""_blank"" class=""Adjunto"">aquí</a><span>";
                    lblRemitente.Text = Correo.Remitente;
                    try
                    {
                        String RutaCorreo = (@"/Temp/Correo" + id.ToString() + (true ? ".html" : ".txt"));
                        iframe.Attributes["src"] = RutaCorreo;
                        File.WriteAllText(MapPath(RutaCorreo), Mensaje, System.Text.Encoding.UTF8);
                    }
                    catch { }
                }
            }
            catch (Exception Ex)
            {
                Ex.Registrar();
            }
        }
Пример #3
0
        public static List <MsgSummary> GetMail(string hostname, int port, string useSsl, string username, string password)
        {
            int    messageCount = 0;
            String fromMail     = "";
            String subject      = "";
            bool   ssl          = false;

            switch (useSsl)
            {
            case "none":
                ssl = false;
                break;

            case "SSL":
                ssl = true;
                break;

            case "TLS":
                ssl = true;
                break;
            }
            List <MsgSummary> allMessages = new List <MsgSummary>();
            Pop3Client        client      = new Pop3Client();

            try
            {
                client.Connect(hostname, port, ssl);
                if (client.Connected)
                {
                    client.Authenticate(username, password);
                }
                messageCount = client.GetMessageCount();
                if (messageCount > 0)
                {
                    for (int i = messageCount; i > 0; i--)
                    {
                        if (client.GetMessage(i) != null)
                        {
                            OpenPop.Mime.Message msg     = client.GetMessage(i);
                            MsgSummary           newSumm = new MsgSummary();
                            try
                            {
                                if (msg.Headers.From.MailAddress.Address != null && msg.Headers.From.MailAddress.Address != "")
                                {
                                    fromMail = msg.Headers.From.MailAddress.Address;
                                }
                                if (msg.Headers.Subject != null && msg.Headers.Subject != "")
                                {
                                    subject = msg.Headers.Subject;
                                }
                                List <MsgAtt> att = new List <MsgAtt>();
                                foreach (OpenPop.Mime.MessagePart attach in msg.FindAllAttachments())
                                {
                                    string file_name_attach = attach.FileName;
                                    if (attach.FileName != "(no name)")
                                    {
                                        FileInfo fi    = new FileInfo(Path.Combine(Globals.TempPath, file_name_attach));
                                        int      ij    = 0;
                                        string   sName = fi.Name;
                                        string   sExt  = fi.Extension;

                                        while (File.Exists(sName))
                                        {
                                            sName = fi.Name + ij.ToString() + sExt;
                                            ij++;
                                        }
                                        fi = new FileInfo(Path.Combine(Globals.TempPath, sName));
                                        att.Add(new MsgAtt()
                                        {
                                            AttFilename = fi
                                        });

                                        attach.Save(fi);
                                    }
                                }
                                newSumm.MsgFile    = att;
                                newSumm.MsgFrom    = fromMail;
                                newSumm.MsgSubject = subject;
                                newSumm.MsgStatus  = "Ok";
                                newSumm.MsgId      = i;
                            }
                            catch (Exception exAtt)
                            {
                                // Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Error Retrieving Email message " + i.ToString() + " of " + messageCount.ToString()+ " Error was: " + exAtt.Message + Environment.NewLine, Color.Red);
                                newSumm.MsgStatus = exAtt.Message;
                            }
                            finally
                            {
                                allMessages.Add(newSumm);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MsgSummary newSumm = new MsgSummary();
                newSumm.MsgStatus = ex.Message;
                allMessages.Add(newSumm);
            }
            finally
            {
                try
                {
                    client.Disconnect();
                }
                catch (Exception ex)
                {
                }
            }
            return(allMessages);
        }