2

I am trying to send email using SendGrid on Azure. I followed these instructions: https://learn.microsoft.com/tr-tr/azure/sendgrid-dotnet-how-to-send-email ; but it doesn't seem to work. . Sending via smtp works though.

using System;
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;

namespace Example
{
    internal class Example
    {
        private static void Main()
        {
            Execute().Wait();
        }

        static async Task Execute()
        {
            var apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
            var client = new SendGridClient(apiKey);
            var msg = new SendGridMessage()
            {
                From = new EmailAddress("from@hotmail.com", "DX Team"),
                Subject = "Hello World from the SendGrid CSharp SDK!",
                PlainTextContent = "Hello, Email!",
                HtmlContent = "<strong>Hello, Email!</strong>"
            };
            msg.AddTo(new EmailAddress("to@gmail.com", "Test User"));
            var response = await client.SendEmailAsync(msg);
        }
    }
}
bit
  • 4,407
  • 1
  • 28
  • 50
Ridianod
  • 35
  • 5

1 Answers1

2

According to your code sample, it would run well while you do not receive any email.

So pay attention to some points as below:

1.When you create api key, make sure that you choose the Full Access. enter image description here 2.You use Gmail to receive email, so set "Allow less secure apps:ON". enter image description here 3.Check the response, if the StatusCode is Accepted it means it send email successfully. enter image description here Then test agian, and it may work well.

Also, you want to make an function which is send email every 15 minutes.

You could refer to the code as below, I create azure function in v1.

public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, [SendGrid(ApiKey = "sendgridkey")] out SendGridMessage message, TraceWriter log)
        {
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
            message = new SendGridMessage();
            message.AddTo("testto@gmail.com");
            message.AddContent("text/html", "Test body");
            message.SetFrom(new EmailAddress("testfrom@gmail.com"));
            message.SetSubject("Subject");

        }
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Thank you so much this detailed answer but I make something wrong I guess. I cant stop program with break point so I cant see any statuscode -_- I'm diging web to fix this or can you share project package with me to understand whats wrong with my code ? Thank you again. Have a nice day – Ridianod Jun 28 '18 at 10:55
  • You need to set your `Sloution Configuration` as `Debug`. And here is how to [debug](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx). – Joey Cai Jun 29 '18 at 01:22
  • Everything gives null :( Also I cant reach my sengridkey too. I dont understand why:/ https://imgur.com/gHpIn7z – Ridianod Jul 05 '18 at 07:03
  • According to the image, you put the sendgridkey in app.config or in appsetting on portal, the way you retrieve key value is correct. And you could refer to this [article](https://learn.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email) for more detail about sendgrid. Calm down, and do the troubleshooting. – Joey Cai Jul 05 '18 at 07:15
  • This article doesnt explain this "App Service" thing on Azure portal. I clicked on App Service and opened some function then added api key. But there are a lot of app service here. Which one do I need to choose ? https://imgur.com/ZdcLEs1 – Ridianod Jul 05 '18 at 07:42
  • Click your function and click the "Application Settings" and add your sendkey like [this](https://i.stack.imgur.com/pMGS2.png). – Joey Cai Jul 05 '18 at 07:53
  • I added this key when I opened this topic :( Checked it and it is here :/ https://imgur.com/mNwk78y But interesting thing is your key less character than mine. hmm – Ridianod Jul 05 '18 at 08:20
  • That's ok. I just do a sample how to add appsettings. – Joey Cai Jul 05 '18 at 08:27
  • I gave my key in string. This time works. First I need to fix why visual studio cant connect to azure and take my api key. Then learn how to add your azure function :D https://imgur.com/EPKOfJH – Ridianod Jul 05 '18 at 08:33
  • When you work in local, you could set your key in local.setting.json file. – Joey Cai Jul 05 '18 at 09:01
  • No, I don't want to work locally. Just tried. Can you tell me which file or code is missing here to connect azure and take the key. https://imgur.com/cQghRQF PS: Sorry I take your time. Thank you for help. – Ridianod Jul 05 '18 at 09:21
  • Have you published the function to azure? – Joey Cai Jul 05 '18 at 09:30
  • And the image you provided is a console app,not a function. – Joey Cai Jul 05 '18 at 09:31
  • Hi Joey. It's me again :( Can you help me to make this scheduled mail on v3. I'm freaking out and didnt find any help to me :( Helpful people send documents to me but I dont understand how to implant this time trigger in mail function. I tried a lot of things but nothing happened. Also tried your V1 function but VS gives error at "SendGridMessage" etc. – Ridianod Jul 09 '18 at 07:13
  • In vs, I could not install v3 function. If you have some new knowledge, glad to share with me to learn. – Joey Cai Jul 09 '18 at 07:18
  • With your code I took this error. VS doesnt show any error. https://imgur.com/cOaSA9Z – Ridianod Jul 09 '18 at 07:56
  • You could try to add your local.setting.json and portal.So that you could test both in local and azure. – Joey Cai Jul 09 '18 at 08:15
  • Hmm. I also realise that there are 2 version here not 3. Finally find where can I choose V1 function. Anyway. I need to learn which code I need to write in local.settings.json. Lets dig it. Thanks. – Ridianod Jul 09 '18 at 08:47
  • Hey Joey! Thank you so much man. Finally I understand what the hell is this Locally thing. I just added "AzureWebJobsSendGridApiKey": "SG.blablabla" and it works. I also learn we can connect storage account in here (local.settings.json). I really bother you but thank you again to continue answer my messages. – Ridianod Jul 09 '18 at 12:47
  • Glad to see you solved the problem. You are welcome. – Joey Cai Jul 10 '18 at 01:24