Reputation: 35
I made this on Azure portal how can I convert to send this scheduled mail in C#?
run.csx looks like;
#r "SendGrid"
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
public static Mail Run(TimerInfo myTimer, TraceWriter log)
{
var today = DateTime.Today.ToShortDateString();
log.Info($"Generating daily report for {today} at {DateTime.Now}");
Mail message = new Mail()
{
Subject = "15 DK'LIK TEST MAILI"
};
Content content = new Content
{
Type = "text/plain",
Value = "Bu mail 15 dk da bir yinelenecektir."
};
message.AddContent(content);
return message;
}
function.json looks like;
{
"bindings": [
{
"type": "timerTrigger",
"name": "myTimer",
"schedule": "0 */15 * * * *",
"direction": "in"
},
{
"type": "sendGrid",
"name": "$return",
"direction": "out",
"apiKey": "CustomSendGridKeyAppSettingName",
"from": "[email protected]",
"to": "[email protected]"
}
],
"disabled": true
}
On C# gives 2 error. I added sendgrid nuget. How Can I pass this errors ? If I just add sengrid mail function in visual studio it gives "run" namespace error. When I copy my portal code in here it has started to give "run" error.
https://i.sstatic.net/ZBt4H.png
Upvotes: 1
Views: 479
Reputation: 35
This is the my solution guys; App; Visual Studio 2017, Installed Azure package from Visual Studio Installer. Then create "Azure Function V1" CS file looks like;
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using SendGrid.Helpers.Mail;
namespace ScheduledMail
{
public static class FifteenMinMail
{
[FunctionName("FifteenMinMail")]
public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, [SendGrid(ApiKey = "AzureWebJobsSendGridApiKey")] out SendGridMessage message, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
message = new SendGridMessage();
message.AddTo("BLABLABLA @gmail or @outlook etc here.");
message.AddContent("text/html", "This mail will repeat every 15 minutes.");
message.SetFrom(new EmailAddress("BLABLABLA @gmail or @outlook etc here."));
message.SetSubject("TEST Mail");
}
}
}
Then dont forget to add your sengrid api key in local.settings.json. Mine looks like;
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"AzureWebJobsSendGridApiKey": "SG.BLABLABLA........"
}
}
Upvotes: 0
Reputation: 4014
You should change your code to
Because C# Methods must be inside a class, And class should be inside namespace
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
namespace YourProject
{
public class TempClass
{
public static Mail Run(TimerInfo myTimer, TraceWriter log)
{
var today = DateTime.Today.ToShortDateString();
log.Info($"Generating daily report for {today} at {DateTime.Now}");
Mail message = new Mail()
{
Subject = "15 DK'LIK TEST MAILI"
};
Content content = new Content
{
Type = "text/plain",
Value = "Bu mail 15 dk da bir yinelenecektir."
};
message.AddContent(content);
return message;
}
}
}
Upvotes: 2