public async Task SendFileFromLinkAsync(string link) { var contentBase64 = await GetFileAsBase64(link); var filename = ExtractFilename(link); var subject = "no-subject"; if (RequiresConversion(filename)) { subject = kindleConvertRequiredText; } System.Collections.Generic.Dictionary <string, string> att = new System.Collections.Generic.Dictionary <string, string>(); att.Add(filename, contentBase64); await emailSender.SendText(emailFrom, kindleEmailTo, subject, "", att); }
// 0 */1 * * * * <- every minute // 0 0 1 ? * SUN * <- every Synday 1 AM public static async Task Run([TimerTrigger("%Lectio2EReaderSchedule%")] TimerInfo myTimer, TraceWriter log, ExecutionContext context) { log.Info($"C# Timer trigger function executed at: {DateTime.Now}"); var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); string sentLinks = ""; bool errorWhenMailing = false; EmailSender mailSender = null; try { mailSender = new EmailSender(config); } catch { errorWhenMailing = true; } try { log.Verbose("Getting list of links ..."); var infoProvider = new LectioInfoProvider(config); var fileLinks = await infoProvider.GetLectioLinks(new LectioInfoProvider.LectioFiles[] { LectioInfoProvider.LectioFiles.RozwazaniaKrotkie, LectioInfoProvider.LectioFiles.LectioMobi }); log.Verbose("Preparing to send to ereader"); var fileSender = new KindleSender(config); foreach (var l in fileLinks) { log.Verbose("Now sending for a link: " + l); await fileSender.SendFileFromLinkAsync(l); sentLinks += l + Environment.NewLine; } try { if (!errorWhenMailing) { await mailSender.SendText(config["KindleEmailTo"], config["NotifyEmailTo"], "Sent to Kindle", sentLinks); } } catch { errorWhenMailing = true; } } catch (Exception ex) { log.Info(ex.Message); if (!errorWhenMailing) { await mailSender.SendText(config["KindleEmailTo"], config["NotifyEmailTo"], "NOT Sent to Kindle", sentLinks + "\r\n" + ex.Message + "\r\n" + ex.StackTrace); } } log.Info($"C# Finished at: {DateTime.Now}"); }