I'm trying to send emails through Googles SMTP OAuth2 API.
I've finanlly managed to get to a state where I am getting an access token without a rejection. Now I am getting the error: Message[Precondition check failed.] Location[ - ] Reason[failedPrecondition] Domain[global]
When I try and send an email through Mime. From my research I see a lot of people saying you need to set your project to have domain wide delegations. The issue is that I am testing on a free account and don't have access to an organisation Gmail account so I can't get into the admin console page and control that setting.
What I need to know is if that is the only stop gap here or is there a way to prove it works, since I am getting back an access token the authorisation seems to work which is the goal but without the email its harder to prove.
Does anyone know if its doable on a free account or needs an organisation account for the domain wide delegation.
I am trying to send an email through the Google API on a free gmail account, it should work but seems to have domain delegation problems
public static async Task GmailAccess()
{
// Replace these values with your own
string serviceAccountEmail ="service account email";
string privateKey = "privatekey";
string userEmail = "[email protected]";
var credential = await CreateGmailCredential(serviceAccountEmail, privateKey, userEmail);
// Use the access token as needed
string accessToken = await credential.GetAccessTokenForRequestAsync();
Console.WriteLine($"Access Token: {accessToken}");
// Sender and recipient email addresses
string senderEmail = "[email protected]";
string recipientEmail = "[email protected]";
// Create a new MimeMessage
var message = new MimeMessage();
message.From.Add(new MailboxAddress("From:", senderEmail));
message.To.Add(new MailboxAddress("To:", recipientEmail));
message.Subject = "OAuth SMTP Email";
// Create the body of the message
var body = new TextPart("plain")
{
Text = "Body of the email."
};
// Attach the body to the message
message.Body = body;
// Send the email using Gmail API
await SendEmailWithGmailAPI(message, userEmail, accessToken);
Console.WriteLine("Email sent successfully.");
}
static async Task<ServiceAccountCredential> CreateGmailCredential(string serviceAccountEmail, string privateKey, string userEmail)
{
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { GmailService.Scope.GmailSend },
}.FromPrivateKey(privateKey));
await credential.RequestAccessTokenAsync(CancellationToken.None);
return credential;
}
Errors here:
static async Task SendEmailWithGmailAPI(MimeMessage message, string userEmail, string accessToken)
{
var rawMessage = Base64UrlEncode(message.ToString());
var gmailMessage = new Google.Apis.Gmail.v1.Data.Message
{
Raw = rawMessage
};
using (var service = new GmailService(new BaseClientService.Initializer()))
{
service.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
try
{
await service.Users.Messages.Send(gmailMessage, "me").ExecuteAsync();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
Means that you are trying to use a service account with Gmail without previously having set up domain wide delegation to your Google Workspace domain or have not added the delegation subject to your code
If you want to use Gmail with a standard Google Gmail user account then you need to use oauth 2 and authorize the user you cannot use a service account