Iam using this code to send email with my web service
string[] Scopes = { GmailService.Scope.GmailSend };
string ApplicationName = "SendMail";
UserCredential credential;
//read credentials file
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None
).Result;
}
string plainText = $"To: [email protected]\r\n" +
$"Subject: subject\r\n" +
"Content-Type: text/html; charset=utf-8\r\n\r\n" +
$"<h1>test email</h1>";
//call gmail service
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var newMsg = new Google.Apis.Gmail.v1.Data.Message();
newMsg.Raw = Base64UrlEncode(plainText.ToString());
service.Users.Messages.Send(newMsg, "me").Execute();
it works totally fine locally after deployment it throws this error
Failed to launch browser with \"https://accounts.google.com/o/oauth2/v2/auth?access_type=offline
even though i added the server url in the redirect uri section and the client's
You are using the Installed App Authorization flow in a Web Server. This is not meant to work. You will have to handle authorization differently in a web hosted application.
In code this translates in using GoogleAuthorizationCodeFlow
instead of GoogleWebAuthorizationBroker
to build the Client instance to authenticate and authorize the Google APIs.
When deploying a Web Application you will have to get the correct type of ClientID first.
Here an extensive guide on how to authenticate Web Server Applications.
Although this is very similar in every Client library, here is an example on how to implement the Server Side authentication flow in the .NET Framework.