0

I'm trying to send emails based on Gmail API. I have created a project in Google Develover Console. I referred to GmailService instance: https://developers.google.com/gmail/api/quickstart/dotnet

And here’s the code that sent an email:

var msg = new AE.Net.Mail.MailMessage { Subject = "Your Subject", Body = "Hello, World, from Gmail API!", From = new MailAddress("[you]@qq.com") };  msg.To.Add(new MailAddress("yourbuddy@qq.com")); 
msg.ReplyTo.Add(msg.From); // Bounces without this!! 
var msgStr = new StringWriter();  
msg.Save(msgStr); 

string[] Scopes = { GmailService.Scope.GmailSend }; 
string ApplicationName = "Gmail API .NET Quickstart"; 
UserCredential credential

using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) 
{ 
string credPath = System.Environment.GetFolderPath( System.Environment.SpecialFolder.Personal); 
credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-quickstart.json"); 

credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; 

Console.WriteLine("Credential file saved to: " + credPath); 
}

var gmail = new GmailService(new Google.Apis.Services.BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); 
var result = gmail.Users.Messages.Send(new Google.Apis.Gmail.v1.Data.Message { Raw = Base64UrlEncode(msgStr.ToString()) }, "user").Execute();

enter image description here But I'm only getting this:

An unhandled exception of type 'Google.GoogleApiException' occurred in Google.Apis.dll Additional information: Google.Apis.Requests.RequestError Bad Request [400] Errors [ Message[Bad Request] Location[ - ] Reason[failedPrecondition] Domain[global] ]

Any idea what is wrong? Thanks!

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Ada Z
  • 1
  • 1

0 Answers0