When you hit send, your app hands the message to your provider's SMTP server, which looks up the recipient's MX record and delivers it to their server.
Receiving servers check that the sending server is allowed to send for your domain, using SPF, DKIM and DMARC. That is why sending through a random SMTP server often lands in spam.