简体   繁体   中英

C# Send Email via Gmail programatically

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/do.net?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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM