The mass of a planet or a star can be infered by observing a body orbiting around it - you have to measure the orbital period (time) and the dimensions of the orbit (radius for circles, semimajor axis for ellipse) and then use Newton's law for gravitation to calculate the gravitating mass. The astronomy textbook example is calculating the masses of two stars that orbit around their common center of mass - the so called binary stars. We know the gravitating mass of the Sun from the periods and distances to its planets.
I think mass of star can be infered from it's spectrum by modelling the processes inside it but that is model dependent, let astronomers say how much its reliable.