Can I send a no reply mail for 'forgot password' functionality in my asp.net core web application? I cannot use SMTP in my web app. Can I use gmail api to send the no-reply mail for sending the forgot password mail? Service accounts or google oauth2.0 can be of any help here?
Asked
Active
Viewed 1,072 times
1
-
Did you try? What issue did you face? – Chetan Oct 18 '21 at 08:15
1 Answers
1
Service accounts can only be used by the gmail api if you have a google workspace domain account.
You will need to set up domain wide delegation to a user on the Doman and the service account will then be able to send emails on its behalf. I guess creating a user called no-reply would be an option in this case.
Code is standard service account authorization code with the addition of adding gsuiteUser to delegate to.
string ApplicationName = "Gmail API .NET Quickstart";
const string serviceAccount = "XXXXX@XXXX-api.iam.gserviceaccount.com";
var certificate = new X509Certificate2(@"c\XXXX-api-ed4859a67674.p12", "notasecret", X509KeyStorageFlags.Exportable);
var gsuiteUser = "domainUser@XXXX.com";
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
{
User = gsuiteUser,
Scopes = new[] { GmailService.Scope.Gmail }
}.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
if (!credential.RequestAccessTokenAsync(CancellationToken.None).Result)
throw new InvalidOperationException("Access token failed.");
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
TBH forgot password functionality should be created on your webiste itself. I would assume that has its own domain and mail server. You should use that and not gmail api.

Linda Lawton - DaImTo
- 106,405
- 32
- 180
- 449