Marlin: Making any GPIO pin HIGH for a given time
As explained in the comments by Tom, you can set any port directly from G-code using the M42 command. To set pin 22 "high", you need to call M42 P22 S255. There is no parameter to add time to the command, so you need to add that yourself using G4 (dwell or pause) to specify how long the printer needs to wait for the next instruction, e.g.:

M42 P22 S255 ; Activate solenoid/relay
G4 P2000     ; Dwell/pause for 2000 milliseconds (2 seconds)
M42 P22 S0   ; Deactivate solenoid/relay


As an alternative, you could use the existing fan if that fan is unused in your machine (or add an "extra fan" in the firmware and send the value 255 to that fan). To use the existing fan:

M106 S255 ; Activate solenoid/relay
G4 P2000  ; Dwell/pause for 2000 milliseconds (2 seconds)
M107      ; Deactivate solenoid/relay


How you add an extra fan is already described (for a specific RAMPS board) in this answer. However, the answer is valid for other boards as well, as long as you have exposed pins you have access to (or if you can solder directly to open pins), you can use the described technique. 

The G-code to activate the "extra fan" (solenoid) is M106 P1 S255 disabling would be M107 P1. Note that there is an option/parameter to add time (Bnnn Blip time - fan will be run at full PWM for this number of seconds when started from standstill) to the M106 command, but that is only implemented in RepRapFirmware. An alternative is to use G4 (dwell or pause) to specify how long the printer needs to wait for the next instruction (M107) is being parsed, e.g.:

M106 P1 S255 ; Activate solenoid/relay
G4 P2000     ; Dwell/pause for 2000 milliseconds (2 seconds)
M107 P1      ; Deactivate solenoid/relay