Exemplo n.º 1
0
        public void mail(string PATH)
        {
            POP3_Wrapper pop = new POP3_Wrapper();

            pop.Connect("pop.qiye.163.com", "*****@*****.**", "abc123", 995, true);
            int s  = pop.GetMessagesCount();
            int cc = s - 50;

            pop.LoadMessages();
            if (!Directory.Exists(PATH))
            {
                Directory.CreateDirectory(PATH);
            }

            for (int i = 0; i < pop.Messages.Count; i++)
            {
                string c = pop.Messages[i].From[0].ToString();

                for (int j = 0; j < pop.Messages[i].Attachments.Count; j++)
                {
                    int    cs = 0;
                    string id = pop.Messages[i].UID;
                    cs = ForecastWarehouseAccessor.count(id);
                    if (cs == 0)
                    {
                        if (pop.Messages[i].From[0].Equals("*****@*****.**"))
                        {
                            int x = ForecastWarehouseAccessor.count2(id);

                            //FileStream fs = new FileStream("D:\\Storage\\UI\\Email\\" + pop.Messages[i].Attachments[j].Text, FileMode.Create);
                            //StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                            //MemoryStream stream = new MemoryStream(b);

                            //FileStream fs = new FileStream("D:\\Storage\\UI\\Email\\" + pop.Messages[i].Attachments[j].Text, FileMode.CreateNew);

                            //fs.Write(pop.Messages[i].Attachments[j].Body, 0, pop.Messages[i].Attachments[j].Body.Length);
                            //fs.Close();

                            System.IO.File.WriteAllBytes(PATH + pop.Messages[i].Subject.Substring(20, 12).ToString() + ".xls", pop.Messages[i].Attachments[j].Body);
                            // System.IO.File.WriteAllBytes("E:\\Storage\\Storage\\UI\\Email\\" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")+i + ".xls", pop.Messages[i].Attachments[j].Body);
                        }
                    }
                }
            }
            //  ComFun.LogTxtAdd("测试2", "进入方法2");
            // LogTxtAdd("测试2", "进入方法2");

            ForecastWarehouseAccessor.bianli(PATH);
        }
Exemplo n.º 2
0
        private static void StartExtractingEmail()
        {
            bool status = false;
            //read config values from database
            string        emailId             = string.Empty;
            string        mfpId               = string.Empty;
            string        mfpIp               = string.Empty;
            string        emailHost           = string.Empty;
            string        emailPort           = string.Empty;
            string        emailUserName       = string.Empty;
            string        emailPassword       = string.Empty;
            string        isRequiredSSl       = string.Empty;
            bool          isDirectPrint       = false;
            string        messageCount        = string.Empty;
            string        ftpIPAddress        = string.Empty;
            string        ftpProtocol         = string.Empty;
            string        ftpPort             = string.Empty;
            string        ftpUserName         = string.Empty;
            string        ftpUserPassword     = string.Empty;
            string        FTPAddress          = string.Empty;
            string        userNameMail        = string.Empty;
            StringBuilder sbFileNames         = new StringBuilder();
            bool          isConnectionSuccess = false;


            DataSet DTwithDuplicate = DataManager.Provider.Device.ProvideEmailSettings();

            for (int emailIndex = 0; emailIndex < DTwithDuplicate.Tables[1].Rows.Count; emailIndex++)
            {
                emailId = DTwithDuplicate.Tables[1].Rows[emailIndex]["EMAIL_ID"].ToString();
                if (!string.IsNullOrEmpty(emailId))
                {
                    var distinctRows = DTwithDuplicate.Tables[0].AsEnumerable().FirstOrDefault(r => r.Field <string>("EMAIL_ID") == emailId);
                    if (distinctRows != null)
                    {
                        mfpId         = distinctRows["MFP_ID"].ToString();
                        mfpIp         = distinctRows["MFP_IP"].ToString();
                        emailHost     = distinctRows["EMAIL_HOST"].ToString();
                        emailPort     = distinctRows["EMAIL_PORT"].ToString();
                        emailUserName = distinctRows["EMAIL_USERNAME"].ToString();
                        emailPassword = distinctRows["EMAIL_PASSWORD"].ToString();
                        if (!string.IsNullOrEmpty(emailPassword))
                        {
                            emailPassword = ProvideDecryptedPassword(emailPassword);
                        }
                        isRequiredSSl = Convert.ToString(distinctRows["EMAIL_REQUIRE_SSL"], CultureInfo.CurrentCulture).ToLower();
                        isDirectPrint = bool.Parse(Convert.ToString(distinctRows["EMAIL_DIRECT_PRINT"], CultureInfo.CurrentCulture));

                        ftpProtocol     = distinctRows["FTP_PROTOCOL"].ToString().ToLower();
                        ftpIPAddress    = distinctRows["FTP_ADDRESS"].ToString();
                        ftpPort         = distinctRows["FTP_PORT"].ToString();
                        ftpUserName     = distinctRows["FTP_USER_ID"].ToString();
                        ftpUserPassword = distinctRows["FTP_USER_PASSWORD"].ToString();
                        if (!string.IsNullOrEmpty(ftpUserPassword))
                        {
                            ftpUserPassword = ProvideDecryptedPassword(ftpUserPassword);
                        }
                        FTPAddress = string.Format("{0}://{1}:{2}", ftpProtocol, ftpIPAddress, ftpPort);


                        try
                        {
                            POP3_Wrapper popWrapper = new POP3_Wrapper();
                            //IMAP_Wrapper popWrapper = new IMAP_Wrapper();
                            string directory      = string.Empty;
                            string folderName     = (string)System.Configuration.ConfigurationManager.AppSettings["folderName"];
                            string emailCountPath = (string)System.Configuration.ConfigurationManager.AppSettings["EmailCountPath"];
                            if (string.IsNullOrEmpty(emailHost) || string.IsNullOrEmpty(emailUserName) || string.IsNullOrEmpty(emailPassword) || string.IsNullOrEmpty(emailPort) || isRequiredSSl != null)
                            {
                                try
                                {
                                    popWrapper.Connect(emailHost, emailUserName, emailPassword, Convert.ToInt32(emailPort), (isRequiredSSl == "true" ? true : false));
                                    isConnectionSuccess = true;
                                }
                                catch (EMailException ex)
                                {
                                    LogManager.RecordMessage(AUDITORSOURCE, "StartExtractingEmail", LogManager.MessageType.Detailed, ex.InnerException.Message, null, ex.InnerException.Message, ex.StackTrace);
                                    isConnectionSuccess = false;
                                }
                                int emailCount = popWrapper.GetMessagesCount();

                                messageCount = DataManager.Provider.Device.GetMessageCount(mfpIp);
                                try
                                {
                                    if (!string.IsNullOrEmpty(messageCount) || messageCount != "0")
                                    {
                                        int messageCountInt = int.Parse(messageCount);
                                        messageCountInt = messageCountInt - 1;
                                        messageCount    = messageCountInt.ToString();
                                    }
                                }
                                catch (Exception)
                                {
                                }

                                if (emailCount > 0)
                                {
                                    string result = DataManager.Controller.Device.UpdateMessageCount(emailId, emailCount.ToString());
                                }
                                if (!string.IsNullOrEmpty(messageCount) && isConnectionSuccess || messageCount != "0" && isConnectionSuccess)
                                {
                                    popWrapper.LoadMessages(messageCount, emailCount.ToString());

                                    foreach (POP3_Message_Wrapper pMW in popWrapper.Messages)
                                    {
                                        foreach (POP3_Mail_Attachment pMA in pMW.Attachments)
                                        {
                                            if (folderName.ToLower() == "username")
                                            {
                                                string[] firstName = pMW.From[0].ToString().Split('@');
                                                userNameMail = firstName[0];
                                            }
                                            else
                                            {
                                                userNameMail = pMW.From[0].ToString();
                                            }

                                            directory = string.Empty;

                                            directory = (string)System.Configuration.ConfigurationManager.AppSettings["DestinationPath"];

                                            directory = directory + "\\" + "EMAIL" + "\\" + userNameMail;

                                            if (!Directory.Exists(directory))
                                            {
                                                Directory.CreateDirectory(directory);
                                            }
                                            string filepathExists = directory + "\\" + pMA.Text;
                                            bool   fileexists     = File.Exists(filepathExists);
                                            if (!fileexists)
                                            {
                                                File.WriteAllBytes(directory + "\\" + pMA.Text, pMA.Body);
                                                status = true;
                                                //Send  Email for new/Existing  user (credentials)

                                                string file = pMA.Text;
                                                sbFileNames.Append(file + ",");



                                                if (isDirectPrint)
                                                {
                                                    FileInfo fileinfo      = new FileInfo(filepathExists);
                                                    string   fileName      = fileinfo.Name;
                                                    string   fileExtension = fileinfo.Extension;
                                                    //if (fileName.Length >= 25)
                                                    //{
                                                    //    fileName = fileName.Substring(0, 25) + fileExtension;
                                                    //}
                                                    FTPAddress = FTPAddress + "//" + fileName;
                                                    UploadFile(filepathExists, FTPAddress, ftpUserName, ftpUserPassword);
                                                }

                                                string[]      pathNamearray = directory.Split('\\');
                                                StringBuilder sbPath        = new StringBuilder();
                                                for (int pathInedx = 0; pathInedx < pathNamearray.Length - 1; pathInedx++)
                                                {
                                                    sbPath.Append(pathNamearray[pathInedx]);
                                                    sbPath.Append("\\");
                                                    directory = sbPath.ToString();
                                                }
                                            }
                                            else
                                            {
                                                string[]      pathNamearray = directory.Split('\\');
                                                StringBuilder sbPath        = new StringBuilder();
                                                for (int pathInedx = 0; pathInedx < pathNamearray.Length - 1; pathInedx++)
                                                {
                                                    sbPath.Append(pathNamearray[pathInedx]);
                                                    if (pathInedx < pathNamearray.Length - 2)
                                                    {
                                                        sbPath.Append("\\");
                                                    }
                                                    directory = sbPath.ToString();
                                                }
                                            }
                                        }
                                        // more investigation reqiured
                                        SendUserCredentials(userNameMail, sbFileNames, mfpIp);
                                    }
                                }
                            }
                        }
                        catch (InvalidCastException invalidCastException)
                        {
                            //When no mail with attachments this exception is thrown
                        }
                        catch (Exception ex)
                        {
                            //LogManager.RecordMessage(AUDITORSOURCE, "StartExtractingEmail", LogManager.MessageType.Exception, ex.Message, null, ex.Message, ex.StackTrace);
                        }
                        finally
                        {
                        }
                    }
                }
            }
        }