Good work everyone, Gmail API also requires user approval. But I have no idea how to use the token. I have to send a control mail to the user. Can I do without tokens? I am making request with HttpClient. I am not using credentials.json. As a postman, I can take and take tokens, but the token must not be renewed. Sorry for my English. Thanks
I tried sending mail with Smtp Server. But it is blocked for security reasons. The only healthy solution is to send mail via Gmail API.
Using SMTP:
public class EmailService
{
public Task Execute(string UserEmail, string Body, string Subject)
{
//enable less secure apps in account google with link
//https://myaccount.google.com/lesssecureapps
try
{
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 1000000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("***email here****", "****password here****");
MailMessage message = new MailMessage("***email here****", UserEmail, Subject, Body);
message.IsBodyHtml = true;
message.BodyEncoding = UTF8Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
client.Send(message);
return Task.CompletedTask;
}
catch (Exception e)
{
Console.Error.Write(e);
return Task.CompletedTask;
}
}
}
Also as mentioned in comment, enable less secure apps in google account. https://myaccount.google.com/lesssecureapps
hint: less secure can't be enabled for emails with 2-step verification.