Пример #1
0
        private string transformFormats(string s)
        {
            Match m;

            if ((m = Regex.Match(s, "=\\?([\\d\\w\\-]+)\\?([QB])\\?(.*?)\\?=")).Success)
            {
                Encoding enc = Encoding.GetEncoding(m.Groups[1].Value);
                switch (m.Groups[2].Value)
                {
                case "Q":                         //Quoted printable
                    s = enc.GetString(Encoding.UTF8.GetBytes(Pop3Utils.FromQuotedPrintable((m.Groups[3].Value))));
                    break;

                case "B":                         //base 64
                    s = enc.GetString(Convert.FromBase64String(m.Groups[3].Value));
                    break;
                }
            }
            return(s);
        }
Пример #2
0
        private void dgmessages_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
            case "CreateActivity":
                ActivityInsert ai          = new ActivityInsert();
                string         mailbody    = HttpUtility.HtmlDecode(Pop3Utils.trimhtml(((Label)e.Item.FindControl("mailbody")).Text));
                string         mailsubject = HttpUtility.HtmlDecode(Pop3Utils.trimhtml(((Label)e.Item.FindControl("mailsubject")).Text));

                TextBox         MailAddressToID = (TextBox)e.Item.FindControl("MailAddressToID");
                RadioButtonList CrossWith       = (RadioButtonList)e.Item.FindControl("CrossWith");

                if (MailAddressToID.Text.Length > 0)
                {
                    string A = String.Empty;
                    string C = String.Empty;
                    string L = String.Empty;

                    switch (CrossWith.SelectedValue)
                    {
                    case "0":
                        A = MailAddressToID.Text;
                        C = String.Empty;
                        L = String.Empty;
                        break;

                    case "1":
                        C = MailAddressToID.Text;
                        A = String.Empty;
                        L = String.Empty;
                        break;

                    case "2":
                        L = MailAddressToID.Text;
                        A = String.Empty;
                        C = String.Empty;
                        break;
                    }
                    if (A.Length > 0 || C.Length > 0 || L.Length > 0)
                    {
                        CheckBox SaveEml = (CheckBox)e.Item.FindControl("SaveEml");
                        long     docid   = 0;
                        if (SaveEml.Checked)
                        {
                            DataRow dtpop3 = DatabaseConnection.CreateDataset("SELECT MAILSERVER,MAILUSER,MAILPASSWORD FROM ACCOUNT WHERE UID=" + UC.UserId).Tables[0].Rows[0];
                            string  pop3   = dtpop3[0].ToString();
                            bool    secure = false;
                            if (pop3.StartsWith("!"))
                            {
                                pop3   = pop3.Substring(1);
                                secure = true;
                            }
                            using (Pop3Client email = new Pop3Client(dtpop3[1].ToString(), dtpop3[2].ToString(), pop3, secure))
                            {
                                email.OpenInbox();
                                email.AttachmentsPath = Path.Combine(ConfigSettings.DataStoragePath, String.Format("webmail{1}{0}{1}", UC.UserId, Path.DirectorySeparatorChar));
                                email.NextEmail(Convert.ToInt64(((Label)e.Item.FindControl("MailMsgId")).Text));
                                string eml = email.Original;

                                Guid g = Guid.NewGuid();
                                using (DigiDapter dg = new DigiDapter())
                                {
                                    dg.Add("Guid", g);
                                    dg.Add("OwnerID", UC.UserId);
                                    dg.Add("IsReview", 0);

                                    dg.Add("ReviewNumber", 0);
                                    dg.Add("HaveRevision", false);
                                    dg.Add("CreatedDate", DateTime.UtcNow);
                                    dg.Add("CreatedByID", UC.UserId);

                                    dg.Add("Filename", email.Subject + ".eml");
                                    dg.Add("size", eml.Length);

                                    dg.Add("Description", email.Subject);
                                    dg.Add("LastModifiedDate", DateTime.UtcNow);
                                    dg.Add("LastModifiedByID", UC.UserId);

                                    dg.Add("TYPE", 0);

                                    dg.Add("Groups", "|" + UC.UserGroupId.ToString() + "|");
                                    object NewID = dg.Execute("FILEMANAGER", DigiDapter.Identities.Identity);
                                    docid = Convert.ToInt64(NewID);
                                }
                                string PathTemplate;
                                PathTemplate = ConfigSettings.DataStoragePath;
                                string NameOfFile = PathTemplate + Path.DirectorySeparatorChar + g.ToString() + ".eml";

                                FileStream   newfile = new FileStream(NameOfFile, FileMode.Create);
                                BinaryWriter wrtfile = new BinaryWriter(newfile);

                                try
                                {
                                    wrtfile.Write(eml);
                                }
                                finally
                                {
                                    wrtfile.Close();
                                    newfile.Close();
                                    email.CloseConnection();
                                }
                            }
                        }

                        ai.InsertActivity("5", "", UC.UserId.ToString(), C, A, L, mailsubject, mailbody, DateTime.UtcNow, UC, 1, false, docid, 0, 0);
                        ClientScript.RegisterStartupScript(this.GetType(), "accretae", "<script>alert('" + Root.rm.GetString("WebMLtxt6") + "');</script>");
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "accretae", "<script>alert('" + Root.rm.GetString("WebMLtxt7").Replace("'", "\'") + "');</script>");
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "accretae", "<script>alert('" + Root.rm.GetString("WebMLtxt7").Replace("'", "\'") + "');</script>");
                }

                break;
            }
        }
Пример #3
0
        private void Repeatermsg_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
            case "DeleteMail":
                try
                {
                    DeleteClick(((Label)e.Item.FindControl("msgid")).Text, ((Label)e.Item.FindControl("msgMessageID")).Text);
                }catch {}
                break;

            case "OpenBody":

                this.dgmessages.Visible = true;
                Repeatermsg.Visible     = false;



                long   msgid     = long.Parse(((Label)e.Item.FindControl("msgid")).Text);
                string MessageID = String.Empty;

                Message[] msgs   = new Message[1];
                ArrayList msgatt = new ArrayList();

                string msgMessageID = ((Label)e.Item.FindControl("msgMessageID")).Text;
                string PathTemplate;
                PathTemplate = Path.Combine(ConfigSettings.DataStoragePath, String.Format("webmail{1}{0}{1}mails", UC.UserId, Path.DirectorySeparatorChar));
                FileFunctions.CheckDir(PathTemplate, true);

                string NameOfFile = PathTemplate + Path.DirectorySeparatorChar + msgMessageID + ".tmsg";
                if (!File.Exists(NameOfFile))
                {
                    DataRow dtPop3 = DatabaseConnection.CreateDataset("SELECT MAILSERVER,MAILUSER,MAILPASSWORD FROM ACCOUNT WHERE UID=" + UC.UserId).Tables[0].Rows[0];
                    string  pop3   = dtPop3[0].ToString();
                    bool    secure = false;
                    if (pop3.StartsWith("!"))
                    {
                        pop3   = pop3.Substring(1);
                        secure = true;
                    }
                    using (Pop3Client email = new Pop3Client(dtPop3[1].ToString(), dtPop3[2].ToString(), pop3, secure))
                    {
                        try
                        {
                            email.OpenInbox();
                            email.AttachmentsPath = Path.Combine(ConfigSettings.DataStoragePath, String.Format("webmail{1}{0}{1}", UC.UserId, Path.DirectorySeparatorChar));
                            email.NextEmail(msgid);

                            try
                            {
                                MessageID = (email.MessageID != null) ? Regex.Replace(email.MessageID.Trim('<', '>'), @"[^a-zA-Z0-9_]", "_") : "";
                            }
                            catch
                            {
                                MessageID = String.Empty;
                            }

                            Message msg = new Message();
                            msg.From    = (email.From != null && email.From.Length > 0) ? email.From : Root.rm.GetString("WebMLtxt11");
                            msg.To      = (email.To != null && email.To.Length > 0) ? email.To : Root.rm.GetString("WebMLtxt11");
                            msg.Subject = (email.Subject != null && email.Subject.Length > 0) ? email.Subject : Root.rm.GetString("WebMLtxt11");
                            msg.MsgID   = msgid;

                            if (email.IsMultipart)
                            {
                                IEnumerator enumerator = email.MultipartEnumerator;
                                Queue       attach     = new Queue();
                                string      bodyhtml   = null;
                                string      bodyplain  = null;
                                while (enumerator.MoveNext())
                                {
                                    Pop3Component multipart = (Pop3Component)enumerator.Current;
                                    if (multipart.IsBody)
                                    {
                                        if (multipart != null && multipart.ContentType.ToLower().IndexOf("text/html") > -1)
                                        {
                                            bodyhtml = multipart.Data;
                                        }
                                        else if (multipart != null && multipart.ContentType.ToLower().IndexOf("text/plain") > -1)
                                        {
                                            bodyplain = multipart.Data;                                                     //multipart.Data.Replace("\r","").Replace("\n","<br>");
                                        }
                                    }
                                    else
                                    {
                                        if (multipart.ContentID != null && multipart.FilePath != null)
                                        {
                                            attach.Enqueue(multipart.ContentID);
                                            attach.Enqueue("/mailinglist/webmail/mailredir.aspx?render=no&att=1&img=" + multipart.FilePath.Replace(email.AttachmentsPath, ""));
                                        }
                                        else if (multipart.FilePath != null)
                                        {
                                            msgatt.Add(multipart.FilePath);
                                        }
                                    }
                                }

                                if (bodyhtml != null)
                                {
                                    while (attach.Count > 0)
                                    {
                                        bodyhtml = Regex.Replace(bodyhtml, @"(?<=src=[""']?)cid:" + ((string)attach.Dequeue()).Trim('<', '>') + @"(?=[""']?)", (string)attach.Dequeue(), RegexOptions.IgnoreCase | RegexOptions.Multiline);
                                    }
                                    bodyhtml = Regex.Replace(bodyhtml, @"</?(html|body|link|meta)[\s\S]*?>", "", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                                    bodyhtml = Regex.Replace(bodyhtml, @"<title[\s\S]*?</title[\s\S]*?>", "<body_not_allowed", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                                    bodyhtml = Regex.Replace(bodyhtml, @"<(style|script|head)[\s\S]*?>", "<!--", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                                    bodyhtml = Regex.Replace(bodyhtml, @"</(style|script|head)[\s\S]*?>", "-->", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                                    bodyhtml = Regex.Replace(bodyhtml, @"(?<=<a)[ ](?=[\s\S]*?>)", " target=_blank ", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                                }

                                if (bodyhtml != null)
                                {
                                    msg.Body = bodyhtml;
                                }
                                else if (bodyplain != null)
                                {
                                    msg.Body = bodyplain.Replace("\r", "").Replace("\n", "<br>");
                                }
                            }
                            else
                            {
                                if (email.IsHTML)
                                {
                                    if (email.ContentTransferEncoding != null && email.ContentTransferEncoding.ToLower().Equals("quoted-printable"))
                                    {
                                        msg.Body = Pop3Utils.FromQuotedPrintable(email.Body);
                                    }
                                    else
                                    {
                                        msg.Body = email.Body;
                                    }

                                    msg.ContentType = "H";
                                }
                                else
                                {
                                    msg.ContentType = "P";
                                    try
                                    {
                                        msg.Body = email.Body.Replace("\r", "").Replace("\n", "<br>");
                                    }
                                    catch
                                    {
                                        msg.Body = String.Empty;
                                    }
                                }
                            }
                            email.CloseConnection();

                            if (msgatt.Count > 0)
                            {
                                MessageAttach[] attach = new MessageAttach[msgatt.Count];
                                for (int i = 0; i < msgatt.Count; i++)
                                {
                                    MessageAttach ma = new MessageAttach();
                                    ma.Link     = msgatt[i].ToString();
                                    ma.Filename = Path.GetFileName(msgatt[i].ToString());
                                    attach[i]   = ma;
                                }
                                msg.Attachment = attach;
                            }
                            msgs[0] = msg;

                            dgmessages.DataSource = msgs;
                            dgmessages.DataBind();
                            tblpaging.Visible = false;

                            if (MessageID.Length > 1)
                            {
                                PathTemplate = Path.Combine(ConfigSettings.DataStoragePath, String.Format("webmail{1}{0}{1}mails", UC.UserId, Path.DirectorySeparatorChar));
                                NameOfFile   = PathTemplate + Path.DirectorySeparatorChar + MessageID + ".tmsg";

                                FileStream      newfile = new FileStream(NameOfFile, FileMode.Create);
                                BinaryFormatter bf      = new BinaryFormatter();
                                bf.Serialize(newfile, msg);
                                newfile.Close();
                            }
                        }
                        catch (Pop3PasswordException)
                        {
                            Context.Items.Add("warning", Root.rm.GetString("Mailtxt16"));
                        }
                        catch (Pop3LoginException)
                        {
                            Context.Items.Add("warning", Root.rm.GetString("Mailtxt17"));
                        }
                        catch (Pop3ConnectException)
                        {
                            Context.Items.Add("warning", Root.rm.GetString("Mailtxt16"));
                        }
                        catch (Pop3DecodeException)
                        {
                            Context.Items.Add("warning", Root.rm.GetString("Mailtxt25"));
                        }
                        catch (Pop3MissingBoundaryException)
                        {
                            Context.Items.Add("warning", Root.rm.GetString("Mailtxt25"));
                        }
                    }
                }
                else
                {
                    Message         msg     = new Message();
                    FileStream      newfile = new FileStream(NameOfFile, FileMode.Open);
                    BinaryFormatter bf      = new BinaryFormatter();
                    msg = (Message)bf.Deserialize(newfile);
                    newfile.Close();

                    msgs[0] = msg;

                    dgmessages.DataSource = msgs;
                    dgmessages.DataBind();
                    tblpaging.Visible = false;
                }
                break;
            }
        }