0

Does anyone know how I can send an email via my webserver (Azure Functions) to gmail?

This is for clients to contact us through a website so it needs to be frictionless and without a challenge.

I have followed this guide

https://developers.google.com/gmail/api/quickstart/dotnet?authuser=1

After creating my own implementation of IDataStore for Azure, I have painfully realised that this will try to challenge via the web browser.

We previously had this working using SMTP using similar method to this

How to send email by using MailKit?

but this requires us to set the 'Allow Un-secure Apps' flag which after a period of time automatically switches off and causes our contact us page to stop working.

I guess this should be possible using an API key which I have created but I can't find any documentation on how to send an email using just the API Key.

Lenny D
  • 1,734
  • 4
  • 22
  • 43
  • Does this answer your question? [How to send email by using MailKit?](https://stackoverflow.com/questions/33496290/how-to-send-email-by-using-mailkit) – Fildor Jul 28 '20 at 09:09
  • @Fildor, no if you look at my question SMTP is not an option because for some reason google keeps reverting 'Allow un-secure apps' after a period of time, which this solution requires. – Lenny D Jul 28 '20 at 09:11
  • Well, my line of thought was it wouldn't be considered "unsecure" anymore if you followed the instructions there? – Fildor Jul 28 '20 at 09:18
  • I don't have a problem with the security of SMTP with SSL again please read my question. After a period of time google automatically reverts the setting and our 'Contact Us' page breaks, we then need to log back in and set the flag again. We can't keep doing that. – Lenny D Jul 28 '20 at 09:27
  • And I guess you also tried to follow this route: https://developers.google.com/gmail/api/quickstart/dotnet – Fildor Jul 28 '20 at 09:32
  • Yes that link is also in my question where I state I have followed it. This method will challenge the user which is no good for me. – Lenny D Jul 28 '20 at 09:35
  • 1
    Looks like I need to go down this route https://developers.google.com/identity/protocols/oauth2 and use google identity platform – Lenny D Jul 28 '20 at 09:36
  • Ah, yes. Wait a second, that was on a different page. I'll look if I can find it again. There is a way to do the authentication in a server-friendly way ... – Fildor Jul 28 '20 at 11:18
  • I had a better one, but cannot find it right now. I found this instead: https://emailarchitect.net/easendmail/sdk/html/object_oauth_service_account.htm tl/dr: Only works with a G Suite account, not personal gmail. Are you on G Suite? – Fildor Jul 28 '20 at 11:22

1 Answers1

0

Gmail api route using google .net client library.

If you are using a gsuite domain email address then you can use a service account. Let me know if you need some code for this.

Otherwise you will need to run your code once locally authorize it stave the refresh token then set up your Idatastore to use the refresh token that you pass to it. As long as the refresh token is used regularly it shouldnt expire. Thats not saying it cant but it shouldnt it will need to be babysat a little if it fails just generate a new refresh token for it and it will be up and running again.

smtp route

if you want to use smtp your probably going to have to work out Xoauth2 I am not aware of anything out of the box for C# that works with that and the Google .net client library does not work with the smpt server.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449