public async Task <ActionResult> SendKheechNotification(int id, string returnFrom) { var kheechEvent = await _context.KheechEvents.Include(k => k.ApplicationUser) .Include(k => k.Location) .FirstOrDefaultAsync(k => k.Id == id); var kheechUsers = await _context.KheechUsers.Include(k => k.ApplicationUser) .Include(k => k.KheechEvent) .Where(k => k.KheechEventId == id) .ToListAsync(); var subject = "Your Friends have invited you to a kheech Event"; var message = $"{kheechEvent.ApplicationUser.FirstName.Humanize()} {kheechEvent.ApplicationUser.LastName.Humanize()} would like you to join her and friends over " + $"{kheechEvent.EventName.Humanize()} at {kheechEvent.Location.Name.Humanize()} on {kheechEvent.StartDate.ToOrdinalWords()}. " + $"Logon to the app for more details and accept the event invite."; var sendGridClient = new SendGridEmailClient(ConfigurationManager.AppSettings["SendGridApiKey"]); foreach (var kuser in kheechUsers) { await sendGridClient.SendEmailAsync(kuser.ApplicationUser.Email, subject, message); } if (returnFrom == "Schedule") { return(RedirectToRoute("HomePage")); } else { return(RedirectToRoute("KheechDetails", new { id = id })); } }
static async Task Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfigurationRoot configuration = builder.Build(); var mongoConnection = configuration.GetConnectionString(database); var apiKey = configuration["EmailService:ApiKey"]; var toAddress = configuration["EmailService:ToAddress"]; var fromAddress = configuration["EmailService:FromAddress"]; var emailConfig = new EmailClientConfiguration(toAddress, fromAddress); var repo = new MongoDbRepository(mongoConnection); var sendGridClient = new SendGridEmailClient(new SendGridClient(apiKey), emailConfig); var submissionsManager = new SubmissionsManager(repo, null); const string loggerTemplate = @"{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}]<{ThreadId}> [{SourceContext:l}] {Message:lj}{NewLine}{Exception}"; var baseDir = "C:/logs/"; var logfile = Path.Combine(baseDir, "notificationslog.txt"); Log.Logger = new LoggerConfiguration() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .Enrich.With(new ThreadIdEnricher()) .Enrich.FromLogContext() .WriteTo.Console(LogEventLevel.Information, loggerTemplate) .WriteTo.File(logfile, LogEventLevel.Information, loggerTemplate, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 10) .CreateLogger(); var serviceProvider = new ServiceCollection() .AddLogging() .AddSingleton <IRepository>(repo) .AddSingleton <ISubmissionsManager>(x => { return(submissionsManager); }) .AddSingleton <IEmailClientConfiguration, EmailClientConfiguration>(x => { return(emailConfig); }) .AddSingleton <IEmailClient, SendGridEmailClient>(x => { return(sendGridClient); }) .AddSingleton <IEmailService, EmailService>(x => { return(new EmailService(sendGridClient, submissionsManager)); }) .BuildServiceProvider(); var svc = serviceProvider.GetService <IEmailService>(); Log.Information("Attmepting to send notifications email."); await svc.TrySendEmailAsync(); Log.Information("Process complete."); }
public async Task <JsonResult> InviteFriend(InviteFriend model) { var currentUserId = User.Identity.GetUserId(); var currentUserName = User.Identity.GetUserName(); if (currentUserName == model.Email) { TempData["InviteMessage"] = $"This is you {model.Email} and already a member of Kheech."; return(Json(new { message = TempData["InviteMessage"] })); } var invitedFriendId = await _context.Users.Where(u => u.Email == model.Email).Select(u => u.Id).FirstOrDefaultAsync(); var isAlreadyFriend = await _context.Friendships.Include(f => f.Recipient) .Include(f => f.Initiator) .FirstOrDefaultAsync(f => (f.InitiatorId == currentUserId && f.RecipientId == invitedFriendId) || (f.InitiatorId == invitedFriendId && f.RecipientId == currentUserId)); if (isAlreadyFriend != null) { TempData["InviteMessage"] = $"You are already friends with {model.Email}"; return(Json(new { message = TempData["InviteMessage"] })); } var invite = new InviteFriend { ApplicationUserId = currentUserId, Email = model.Email, FriendshipStatusId = 1, InsertDate = DateTime.UtcNow }; _context.InviteFriends.Add(invite); await _context.SaveChangesAsync(); var subject = "Invitation to Kheech - Breaking Bread"; var message = $"you are invited to join the best app to achedule a meeting with friends. Please click the link below: {ConfigurationManager.AppSettings["SiteUrlBase"]}"; var sendGridClient = new SendGridEmailClient(ConfigurationManager.AppSettings["SendGridApiKey"]); await sendGridClient.SendEmailAsync(model.Email, subject, message); TempData["InviteMessage"] = "Your invite has been sent."; return(Json(new { result = true, message = TempData["InviteMessage"] })); }
public static async Task EmailResults(string results) { if (string.IsNullOrEmpty(SendGridToken)) { Fail($"Missing {SEND_GRID_API_KEY_NAME}"); } var from = "*****@*****.**"; var to = new List <string>() { "*****@*****.**", "*****@*****.**" }; var subject = "American Apex Update"; var emailClient = new SendGridEmailClient(SendGridToken); bool success = await emailClient.SendEmailAsync(from, to, subject, results, isHtml : false); if (!success) { Fail("Failed to send email"); } }