2

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)

user229044
  • 232,980
  • 40
  • 330
  • 338
D. A.
  • 3,369
  • 3
  • 31
  • 34
  • 1
    It seems that you have [wasted 6 hours](https://stackoverflow.com/questions/33233694/gmail-api-can-i-send-email-using-the-service-account). Sorry, happens in our job – Liquid Core Mar 05 '22 at 18:55
  • 2
    Use the [MailKit](https://github.com/jstedfast/MailKit) package to use the SMTP protocol to send messages. But know this: you may be better off using a mail service provider than trying to hit a Google-operated SMTP server directly. They make it hard to keep spamsters out. Sendgrid has a reasonable service, and there are others. – O. Jones Mar 05 '22 at 18:57
  • Thanks both. I used to use SendGrid. Then after about a year off, they disabled my account and there seems to be no customer service to help. Ho-hum... I'll figure something out. – D. A. Mar 06 '22 at 07:05
  • Does this answer your question? https://stackoverflow.com/a/69612942/1841839 – Linda Lawton - DaImTo Mar 06 '22 at 10:01
  • Service Accounts are part of Google Cloud. Google Cloud does not provide email services. You will need to use a mail service to send mail. You can use Google Workspace and Domain Wide Delegation but that is the wrong approach. I recommend using a mail service that supports your use case (machine generated email). – John Hanley Mar 08 '22 at 17:20

1 Answers1

2

gmail api option.

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.

smtp / imap option.

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I finally got it working, and it was similar enough to this that I will accept it. I also had to add this: ``` GoogleCredential googleCred = GoogleCredential.FromStream(stream) .CreateScoped(Scopes) .CreateWithUser(from_email_addr); ``` – D. A. Mar 08 '22 at 19:38