예제 #1
0
        protected void AutoCheckMailForAccount(Account acct)
        {
            if (acct != null)
            {
                try
                {
                    DbStorage              dbs     = DbStorageCreator.CreateDatabaseStorage(acct);
                    MailProcessor          mp      = new MailProcessor(dbs);
                    WebmailResourceManager _resMan = (new WebmailResourceManagerCreator()).CreateResourceManager();
                    try
                    {
                        mp.MessageDownloaded += new DownloadedMessageHandler(mp_MessageDownloaded);
                        mp.Connect();

                        FolderCollection fc1 = dbs.GetFolders();
                        FolderCollection fc2 = new FolderCollection();
                        foreach (Folder fld in fc1)
                        {
                            if (fld.Type == FolderType.Inbox)
                            {
                                fc2.Add(fld);
                            }
                        }
                        Dictionary <long, string> updatedFolders = mp.Synchronize(fc2);
                        string strFolders = "";
                        foreach (KeyValuePair <long, string> kvp in updatedFolders)
                        {
                            strFolders += "{id: " + kvp.Key.ToString() + ", fullName: '" + kvp.Value + "'}, ";
                        }
                        Response.Write(@"<script type=""text/javascript"">parent.SetUpdatedFolders([" + strFolders.TrimEnd(new char[2] {
                            ',', ' '
                        }) + "], false);</script>");
                    }
                    finally
                    {
                        mp.MessageDownloaded -= new DownloadedMessageHandler(mp_MessageDownloaded);
                        mp.Disconnect();
                    }
                }
                catch (WebMailException ex)
                {
                    Log.WriteException(ex);
                    errorDesc = Utils.EncodeJsSaveString(ex.Message);
                    if (Type == 1 || Type == 2)
                    {
                        Session.Add(Constants.sessionErrorText, errorDesc);
                    }
                }
            }
        }
예제 #2
0
 protected void CheckMailForAccount(Account acct)
 {
     if (acct != null)
     {
         try
         {
             DbStorage              dbs     = DbStorageCreator.CreateDatabaseStorage(acct);
             MailProcessor          mp      = new MailProcessor(dbs);
             WebmailResourceManager _resMan = (new WebmailResourceManagerCreator()).CreateResourceManager();
             try
             {
                 mp.MessageDownloaded += new DownloadedMessageHandler(mp_MessageDownloaded);
                 Response.Write(@"<script type=""text/javascript"">parent.SetStateTextHandler(""" + _resMan.GetString("LoggingToServer") + @""");</script>");
                 mp.Connect();
                 Response.Write(@"<script type=""text/javascript"">parent.SetStateTextHandler(""" + _resMan.GetString("GettingMsgsNum") + @""");</script>");
                 Dictionary <long, string> updatedFolders = mp.Synchronize(dbs.GetFolders());
                 if (Type == 0)
                 {
                     string strFolders = "";
                     foreach (KeyValuePair <long, string> kvp in updatedFolders)
                     {
                         strFolders += "{id: " + kvp.Key.ToString() + ", fullName: '" + kvp.Value + "'}, ";
                     }
                     Response.Write(@"<script type=""text/javascript"">parent.SetUpdatedFolders([" + strFolders.TrimEnd(new char[2] {
                         ',', ' '
                     }) + "]);</script>");
                 }
             }
             finally
             {
                 mp.MessageDownloaded -= new DownloadedMessageHandler(mp_MessageDownloaded);
                 Response.Write(@"<script type=""text/javascript"">parent.SetStateTextHandler(""" + _resMan.GetString("LoggingOffFromServer") + @""");</script>");
                 mp.Disconnect();
             }
         }
         catch (WebMailException ex)
         {
             Log.WriteException(ex);
             errorDesc = Utils.EncodeJsSaveString(ex.Message);
             if (Type == 1)
             {
                 Session.Add(Constants.sessionErrorText, errorDesc);
             }
         }
     }
 }
예제 #3
0
        internal static System.Xml.XmlAttribute CreateInboxSyncAttr(XmlElement webmailNode, Account acct)
        {
            XmlAttribute   inboxSyncTypeAttr = webmailNode.OwnerDocument.CreateAttribute("inbox_sync_type");
            FolderSyncType inboxSyncType     = FolderSyncType.NewHeadersOnly;
            MailProcessor  mp = new MailProcessor(DbStorageCreator.CreateDatabaseStorage(acct));

            try
            {
                mp.Connect();
                Folder fld = mp.GetFolder(FolderType.Inbox);
                if (fld != null)
                {
                    inboxSyncType = fld.SyncType;
                }
            }
            finally
            {
                mp.Disconnect();
            }
            inboxSyncTypeAttr.Value = ((short)inboxSyncType).ToString(CultureInfo.InvariantCulture);

            return(inboxSyncTypeAttr);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            acct = Session[Constants.sessionAccount] as Account;
            if (acct == null)
            {
                Response.Redirect("default.aspx", true);
            }

            string userAgent;             //Client browser
            string temp_filename = Request.QueryString["temp_filename"];

            if (temp_filename != null)
            {
                try
                {
                    byte[] buffer     = new byte[0];
                    object tempFolder = Utils.GetTempFolderName(Session);
                    if (tempFolder != null)
                    {
                        //Response.Write(tempFolder.ToString() + "<br>");
                        //Response.Write(temp_filename);
                        //Response.End();
                        string safe_temp_file_name = Path.GetFileName(temp_filename);

                        string fullPath = Path.Combine(tempFolder.ToString(), safe_temp_file_name);
                        if (File.Exists(fullPath))
                        {
                            using (FileStream fs = File.OpenRead(fullPath))
                            {
                                buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, buffer.Length);
                            }
                        }
                    }
                    else
                    {
                        return;
                    }
                    string filename = Request.QueryString["filename"] ?? temp_filename;
                    string download = Request.QueryString["download"];
                    //*************************************************************
                    //IE with cyrillic file names
                    //*************************************************************
                    string encodedFilename;
                    userAgent = Request.UserAgent;
                    if (userAgent.IndexOf("MSIE") > -1)
                    {
                        encodedFilename = Server.UrlPathEncode(filename);
                    }
                    else
                    {
                        encodedFilename = filename;
                    }
                    //**************************************************************
                    if (download != null)
                    {
                        Response.Clear();

                        if (string.Compare(download, "1", true, CultureInfo.InvariantCulture) == 0)
                        {
                            Response.AddHeader("Content-Disposition", @"attachment; filename=""" + encodedFilename + @"""");
                            Response.AddHeader("Accept-Ranges", "bytes");
                            Response.AddHeader("Content-Length", buffer.Length.ToString(CultureInfo.InvariantCulture));
                            Response.AddHeader("Content-Transfer-Encoding", "binary");
                            Response.ContentType = "application/octet-stream";
                        }
                        else
                        {
                            string ext = Path.GetExtension(filename);
                            if (!string.IsNullOrEmpty(ext))
                            {
                                ext = ext.Substring(1, ext.Length - 1);                                 // remove first dot
                            }
                            Response.ContentType = Utils.GetAttachmentMimeTypeFromFileExtension(ext);
                        }
                    }
                    Response.BinaryWrite(buffer);
                    Response.Flush();
                }
                catch (Exception ex)
                {
                    Log.WriteException(ex);
                }
            }
            else
            {
                if (Request.QueryString["partID"] != null)
                {
                    byte[] buffer = new byte[0];

                    try
                    {
                        string uid = HttpUtility.UrlDecode(Request.QueryString["uid"]);
                        string full_folder_name = Request.QueryString["full_folder_name"];
                        string partID           = Request.QueryString["partID"];
                        string filename         = Request.QueryString["filename"];
                        string download         = Request.QueryString["download"];
                        string temp_file        = Request.QueryString["tmp_filename"];
                        string temp_folder      = Utils.GetTempFolderName(Session);

                        //*************************************************************
                        //IE with cyrillic file names
                        //*************************************************************
                        string encodedFilename;
                        userAgent = Request.UserAgent;
                        if (userAgent.IndexOf("MSIE") > -1)
                        {
                            encodedFilename = Server.UrlPathEncode(filename);
                        }
                        else
                        {
                            encodedFilename = filename;
                        }
                        //**************************************************************

                        if (!string.IsNullOrEmpty(temp_file) && File.Exists(Path.Combine(temp_folder, temp_file)))
                        {
                            using (FileStream fs = File.OpenRead(Path.Combine(temp_folder, temp_file)))
                            {
                                buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, buffer.Length);
                            }
                        }
                        else
                        {
                            MailProcessor mp = new MailProcessor(DbStorageCreator.CreateDatabaseStorage(Session[Constants.sessionAccount] as Account));
                            try
                            {
                                mp.Connect();
                                Folder fld = mp.GetFolder(full_folder_name);
                                if (fld != null)
                                {
                                    buffer = mp.GetAttachmentPart(uid, fld, partID);
                                }
                            }
                            finally
                            {
                                mp.Disconnect();
                            }
                            string tmp_filename = Utils.CreateTempFilePath(temp_folder, filename, true);
                            File.WriteAllBytes(tmp_filename, buffer);
                        }


                        if (download != null)
                        {
                            Response.Clear();

                            if (string.Compare(download, "1", true, CultureInfo.InvariantCulture) == 0)
                            {
                                Response.AddHeader("Content-Disposition", @"attachment; filename=""" + encodedFilename + @"""");
                                Response.AddHeader("Accept-Ranges", "bytes");
                                Response.AddHeader("Content-Length", buffer.Length.ToString(CultureInfo.InvariantCulture));
                                Response.AddHeader("Content-Transfer-Encoding", "binary");
                                Response.ContentType = "application/octet-stream";
                            }
                            else
                            {
                                string ext = Path.GetExtension(filename);
                                if (!string.IsNullOrEmpty(ext))
                                {
                                    ext = ext.Substring(1, ext.Length - 1); // remove first dot
                                }
                                Response.ContentType = Utils.GetAttachmentMimeTypeFromFileExtension(ext);
                            }
                        }

                        Response.BinaryWrite(buffer);
                        Response.Flush();
                    }
                    catch (Exception ex)
                    {
                        Log.WriteException(ex);
                    }
                }
                else if ((Request.QueryString["id_msg"] != null) &&
                         (Request.QueryString["uid"] != null) &&
                         (Request.QueryString["id_folder"] != null) &&
                         (Request.QueryString["folder_path"] != null))
                {
                    try
                    {
                        int  id_msg    = int.Parse(Request.QueryString["id_msg"], CultureInfo.InvariantCulture);
                        long id_folder = long.Parse(Request.QueryString["id_folder"], CultureInfo.InvariantCulture);

                        WebMailMessage msg = null;
                        MailProcessor  mp  = new MailProcessor(DbStorageCreator.CreateDatabaseStorage(Session[Constants.sessionAccount] as Account));
                        try
                        {
                            mp.Connect();
                            Folder fld = mp.GetFolder(id_folder);
                            if (fld != null)
                            {
                                msg = mp.GetMessage((fld.SyncType != FolderSyncType.DirectMode) ? (object)id_msg : HttpUtility.UrlDecode(Request.QueryString["uid"]), fld);
                            }
                        }
                        finally
                        {
                            mp.Disconnect();
                        }

                        if (msg.MailBeeMessage != null)
                        {
                            string subj = msg.MailBeeMessage.Subject;
                            //«\», «/», «?», «|», «*», «<», «>», «:»
                            string safeSubject = string.Empty;
                            for (int i = 0; i < subj.Length; i++)
                            {
                                if (subj[i] == '\\' || subj[i] == '|' || subj[i] == '/' ||
                                    subj[i] == '?' || subj[i] == '*' || subj[i] == '<' ||
                                    subj[i] == '>' || subj[i] == ':')
                                {
                                    continue;
                                }
                                safeSubject += subj[i];
                            }
                            safeSubject = safeSubject.TrimStart();
                            if (safeSubject.Length > 30)
                            {
                                safeSubject = safeSubject.Substring(0, 30).TrimEnd();
                            }
                            safeSubject = safeSubject.TrimEnd(new char[2] {
                                '.', ' '
                            });
                            if (safeSubject.Length == 0)
                            {
                                safeSubject = "message";
                            }

                            string encodedMsgFilename;
                            userAgent = Request.UserAgent;
                            if (userAgent.IndexOf("MSIE") > -1)
                            {
                                encodedMsgFilename = Server.UrlPathEncode(safeSubject);
                                Response.AddHeader("Expires", "0");
                                Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
                                Response.AddHeader("Pragma", "public");
                            }
                            else
                            {
                                encodedMsgFilename = safeSubject;
                            }
                            //**************************************************************
                            byte[] buffer = msg.MailBeeMessage.GetMessageRawData();
                            //**************************************************************
                            Response.Clear();
                            Response.ContentType = "application/octet-stream";
                            Response.AddHeader("Accept-Ranges", "bytes");
                            Response.AddHeader("Content-Length", buffer.Length.ToString(CultureInfo.InvariantCulture));
                            Response.AddHeader("Content-Disposition", string.Format(@"attachment; filename=""{0}.eml""", encodedMsgFilename));
                            Response.AddHeader("Content-Transfer-Encoding", "binary");
                            Response.BinaryWrite(buffer);
                            Response.Flush();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteException(ex);
                    }
                }
            }
        }