Satellite network is considered a prominent architecture for future space-air-ground integrated networks, where the relay satellite network (RSN) plays an essential role in data transmission. However, the relay satellite network faces practical challenges. First, due to the dynamic network topology and limited resources such as storage capacity, antennas, and bandwidth, optimizing data transmission and resource allocation in a resource-constrained stochastic system is challenging. Second, satellites intermittently enter the eclipse zone, which leads to an unstable energy supply. Therefore, optimal energy management is required to balance energy supply and consumption. In this paper, we proposed an online optimal control algorithm, based on Lyapunov stability theory, to optimize data transmission and energy management jointly. Furthermore, the performance of our proposal is analyzed comprehensively, including the reasonable bounds for data and energy and the optimality of our proposed algorithm. Finally, extensive performance evaluation simulations are performed based on real-world satellite parameters. The simulation results indicate that the proposed method can achieve long-term network stability and energy sustainability. Moreover, the proposed algorithm shows an improvement of 9.6% and 20.3% in system utility compared to the non-optimized transmission method and non-optimized energy management algorithm, respectively.