I have a Blazor application. I'd like to send an email from the server side (for example, when a user registers). I have setup a service account within my gmail organization and have a user in my organization that will be the 'from' address. Can someone point me at the right documentation for this use case? Or an example? I do not want any popup/web authentication step. (The email is sent from the server side, with no GUI.)
(I recognize this as a potentially vague question... I'm not sure what to ask, and the support docs suggested using stack overflow. shrug)
In order to use service accounts with your workspace domain you need to set up the service account and enable domain wide deligation
Google has sevral very good guidles on how to set it up. The best IMO beingPerform Google Workspace Domain-Wide Delegation of Authority
You will just need to rember to set the scope https://www.googleapis.com/auth/gmail
, instead of https://www.googleapis.com/auth/admin.directory.user
, https://www.googleapis.com/auth/admin.directory.group
shown in the example
After that its simply a matter of using the standard service account code and adding the user that you wish to deligate as
var credential = GoogleCredential.FromFile(serviceAccountCredentialFilePath)
.CreateScoped(scopes);
var gsuiteUser = "noreply@yourdomain.com";
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
This is a previous question related to this. How to use gmail api with service accounts or google Oauth in C# for sending mails? However im not sure if you are looking for a smtp answer or a Gmail api answer.
If you want to go though the SMTP / IMAP you need to check Xoauth2. The procedure is the same you will need to set up domain wide delegation to your domain account but the scope changes you need to use https://www.googleapis.com/auth/gmail.imap_admin
I am not aware of any .net examples of this.