On my taillamp, there were several problems.
Firstly, find and clean the connector under the seat that goes to the tail lamp.
Clean & file the ground under the mudguard bolt that holds the taillight assembly.
Check the junctions on the bulb holder of the tail light... mine has one that was very loose and which could cause a short circuit by touching the other, I had to bend it to a better shape.
Clean and sand all the copper junctions on the bulb unit.
Lastly, use pliers to "crimp" (gently) the connectors that plug onto the tailiight bulb assembly... they can easily work loose.
Ditto fredgold and the front connectors.
The rear brake light switch is in the toolbox, check the connector there and the switch itself (the spring might be disconnected).
Good luck!