Wim ten Brink
Wim ten Brink

Reputation: 26682

How to send emails through Google GSuite as an App?

I am administrator for a GSuite account with 10 users and I've a simple console application that reads all users, removes all old aliases for each user and then auto-creates new aliases for every user. This is needed when a new domain is added or an old domain is removed. Thus if example.com is removed and example.org is added then it would remove the [email protected] alias and add the [email protected] alias. Just a simple management tool.
It needs to send a separate email to each and every primary address and alias as users might have an email client that combines duplicate emails into a single one. They want to see an email for every new alias they receive as confirmation that it works. (Which is why their alias is in the subject of each email.)
After all aliases have been adjusted, I want to send an email to each primary address and each alias. Each email will have a short subject mentioning the user name and the email address that should receive it. The body of each email will have some personal information, their primary address, their old and new aliases and for each alias a notification if it has been added or deleted. And a short description of why the aliases have been removed. This doesn't happen often and there aren't more than 25 aliases per user, but it's still around 250 emails that it needs to send. And send fast.
Now, the problem is not the email itself. Just create a message, put the alias as recipient with a body and me as admin as the sender. New SmtpClient component with smtp.gmail.com as host and port 587 for the TSL, and basically this:

using SmtpClient client = new SmtpClient("smtp.ziggo.nl", 587)
{
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(sender.Address, password)
};

But this only works if I allow less secure applications in GSuite, and I don't want to turn that on. Not even temporarily. So I need a workaround. I've been reading about using an App password but can't find how to set that up. Stack Overflow has plenty of similar questions yet none explain how to set up this App password. I've seen some information in Google's helpfiles and the admin console about setting up Android and IOS apps but this is just a simple Win64 console application. It's meant to run after any changes in the GSuite domains.
I have an alternate method, which is by using my provider account, which works just fine and is capable of sending out the 250 emails. But Google seems to be throttling my emails or causing other problems probably because it considers the app less secure. So, how do I turn it into a secure console app?
Without the use of third-party components, btw. It is likely something very trivial that I'm missing...

Upvotes: 3

Views: 1949

Answers (1)

Ogglas
Ogglas

Reputation: 69968

First things first, if you are using Advanced Protection Program you can't use App passwords.

Can I still use App passwords?

No. Apps that use App passwords instead of 2-Step Verification are blocked for users with Advanced Protection.

https://support.google.com/accounts/answer/7539956?hl=en

Sending emails from an App Google recommends SMTP relay service for Google Workspace.

  1. Sign in to your Google Admin console.

  2. From the Admin console Home page, go to Apps -> Google Workspace -> Gmail -> Routing. Note: You might find this setting at Apps -> Google Workspace -> Gmail -> Advanced Settings.

  3. Next to SMTP relay service, click Configure.

  4. Set up the SMTP relay service by following the steps in SMTP relay: Route outgoing non-Gmail messages through Google.

On your device or in your app, connect to smtp-relay.gmail.com on one of these ports: 25, 465, or 587.

https://support.google.com/a/answer/176600?hl=en

If you would rather use App Passwords, although not recommended, you do it like this.

  1. Select Security.

  2. Under "Signing in to Google," select App Passwords. You may need to sign in. If you don't have this option, it might be because:

    a. 2-Step Verification is not set up for your account.

    b. 2-Step Verification is only set up for security keys.

    c. Your account is through work, school, or other organization.

    d. You turned on Advanced Protection.

  3. At the bottom, choose Select app and choose the app you using -> Select device and choose the device you're using -> Generate.

  4. Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device.

  5. Tap Done.

https://support.google.com/mail/answer/185833?hl=en

You can monitor application specific passwords like this, see "View and revoke application-specific passwords":

https://support.google.com/a/answer/2537800?hl=en

Upvotes: 2

Related Questions