Our business uses G Suite for its emails. Currently we have a number of automated emails that are sent using MailKit
in .Net Core code. This has been working fine until yesterday where we had to send a large number of emails (around 600) in one go. Around 60 emails were sent successfully, but the rest all failed with errors like this which were returned from Gmail:
4.7.0 Temporary System Problem. Try again later (10). i4sm4226208wrr.17 - gsmtp
Which I believe is Gmail telling me it’s been overloaded: https://stackoverflow.com/a/39108563/5392786
I’m guessing Google has a restriction on how many emails you can send in this way?
A lot of our infrastructure is on AWS so I have considered switching to AWS SES to handle our automated emails. Is this a viable option?
I still want the rest of the business to be able to continue using Gmail as their email client. Is it possible to use AWS SES in parallel with Gmail for sending emails from code (using the AWS SDK), but leave the receiving of emails as it is (i.e. SES doesn’t have anything to do with receiving or handling incoming emails, it just sends emails when I tell it to from code)?
EDIT Here's the code I'm using to send the emails:
public async Task SendEmail(MimeMessage message)
{
var certificiate = new X509Certificate2("certificate.p12", "notasecret", X509KeyStorageFlags.Exportable);
var credentials = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer("automatedemails@automatedemails-######.iam.gserviceaccount.com")
{
Scopes = new[] { GmailService.Scope.MailGoogleCom },
User = ((MailboxAddress)message.From.First()).Address
}.FromCertificate(certificiate));
if (!await credentials.RequestAccessTokenAsync(new CancellationToken()))
{
throw new ApplicationException("Error requesting access token for Gmail authentication");
}
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
var oauth2 = new SaslMechanismOAuth2(credentials.User, credentials.Token.AccessToken);
client.Authenticate(oauth2);
await client.SendAsync(message);
client.Disconnect(true);
}
}
This is run for every email. I wonder if it's because I'm opening and closing the connection for every email?