They have to be a member of the channel/Team. You can do it programmatically via the Graph API: https://learn.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-1.0&tabs=http
But the user you'll be running this under still needs to be a member of the channel/team.