1

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": "blabla@hotmail.com",
      "to": "blabla@hotmail.com"
    }
  ],
  "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.stack.imgur.com/ZBt4H.png

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Ridianod
  • 35
  • 5
  • Please note that Visual Studio is the name of a code editor (like Notepad is a text editor, or Excel is a spreadsheet editor). The `visual-studio` tag should only be used for questions pertaining to the editor itself, and not code that has merely been written using Visual Studio. – ProgrammingLlama Jul 06 '18 at 07:23
  • You have placed your code directly into a `.cs` file and not into a namespace and class. – ProgrammingLlama Jul 06 '18 at 07:24
  • Possible duplicate of ["A namespace cannot directly contain members such as fields or methods" in Net.Reflector](https://stackoverflow.com/questions/21175781/a-namespace-cannot-directly-contain-members-such-as-fields-or-methods-in-net-r) – ProgrammingLlama Jul 06 '18 at 07:25
  • 1
    Oh. Thank you so much. I'm newbie. I didnt notice there was no class in this code :D – Ridianod Jul 06 '18 at 08:01
  • Ridanod You're welcome :) Good luck with your project. – ProgrammingLlama Jul 06 '18 at 08:01
  • Thank you but this time I took no jobs error. – Ridianod Jul 06 '18 at 13:49
  • Hi Ridianod, it's best to create a new question as it's a new problem. You shouldn't edit your question so radically, especially after it has attracted answers. – ProgrammingLlama Jul 06 '18 at 14:33

2 Answers2

2

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;
        }
    }
}
Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
0

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........"
  }
}
Ridianod
  • 35
  • 5