1

It sends the emails locally, and I can confirm them and then everything is just fine and dandy. After deployment, it still gives me the "please check your email" screen, but the email isn't actually sending. I did open up port 587 manually on IIS thinking that maybe this was blocking it, but identifying the actual root of the cause is escaping me. Apologies if this has been asked before, I'm new to this framework and am trying not to ask stupid questions. I sincerely appreciate any help! Also, code that I included is where I assume it's coming from but I can't really be sure. I appreciate it. EDIT I found that it is in fact still keeping the correct email so I'm assuming something on the server could be blocking this?

using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Options;
using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;

namespace O3is_v1.Models
{
        public class SendGridEmailSender : IEmailSender
        {
            public SendGridEmailSender(
                       IOptions<SendGridEmailSenderOptions> options
                       )
            {
                this.Options = options.Value;
            }

            public SendGridEmailSenderOptions Options { get; set; }

            public async Task SendEmailAsync(
                string email,
                string subject,
                string message)
            {
                await Execute(Options.ApiKey, subject, message, email);
            }

            private async Task<Response> Execute(
                string apiKey,
                string subject,
                string message,
                string email)
            {
                var client = new SendGridClient(apiKey);
                var msg = new SendGridMessage()
                {
                    From = new EmailAddress(Options.SenderEmail, Options.SenderName),
                    Subject = subject,
                    PlainTextContent = message,
                    HtmlContent = message
                };
                msg.AddTo(new EmailAddress(email));

                // disable tracking settings
                // ref.: https://sendgrid.com/docs/User_Guide/Settings/tracking.html
                msg.SetClickTracking(false, false);
                msg.SetOpenTracking(false);
                msg.SetGoogleAnalytics(false);
                msg.SetSubscriptionTracking(false);

                return await client.SendEmailAsync(msg);
            }
        }
    }
  • I think your question can be sum up as: codes working well in local side, but after publishing to server/IIS, the email didn't send. So I think [this document](https://docs.sendgrid.com/for-developers/sending-email/iis75) may help you, take a try? – Tiny Wang Jan 18 '22 at 06:28
  • Thanks for the response Tiny, still no luck. – oracleofoz920 Jan 18 '22 at 20:43
  • If you got any error message or log after you published it to the server? I assume that error message may help. – Tiny Wang Jan 19 '22 at 01:44
  • 1
    I would think so too! But my error logs aren't showing that much detail (actually any error) related to the emails not sending. I'm looking into how to get more information on them. I will update. Also, thank you so much for at least trying to help me. I'm trying a couple things and I'll get back to you! – oracleofoz920 Jan 19 '22 at 02:07
  • Thanks sir, and I think we really need error information to troubleshoot the issue. – Tiny Wang Jan 19 '22 at 02:08
  • 1
    I think I've got it figured out.. A major part of the issue I was experiencing was my application wasn't reading my environment variables when deployed... So, I changed settings in Application Pool... That didn't work... so I found this https://aws.amazon.com/blogs/developer/environment-variables-with-net-core-and-elastic-beanstalk/.. Boom environment variables working :) – oracleofoz920 Jan 19 '22 at 02:45
  • Thanks for your reply sir, you can post it as the answer to end this ticket. – Tiny Wang Jan 19 '22 at 02:46

1 Answers1

0

I think I've got it figured out.

A major part of the issue I was experiencing was that my application wasn't reading my environment variables when deployed. Initially, I changed the settings in the Application Pool—but that didn't work. I also tried following Amazon's documentation for Custom Environment Variables in the AWS Cloud9 IDE, but that proved insufficient; the environment variables show up, but the (ASP.NET MVC) application wasn't reading them.

That all said, I was able to find an implementation elsewhere on Stack Overflow that does work, which proposes adding the following code:

private static void SetEbConfig()
{
    var tempConfigBuilder = new ConfigurationBuilder();

    tempConfigBuilder.AddJsonFile(
        @"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration",
        optional: true,
        reloadOnChange: true
    );

    var configuration = tempConfigBuilder.Build();

    var ebEnv =
        configuration.GetSection("iis:env")
            .GetChildren()
            .Select(pair => pair.Value.Split(new[] { '=' }, 2))
            .ToDictionary(keypair => keypair[0], keypair => keypair[1]);

    foreach (var keyVal in ebEnv)
    {
        Environment.SetEnvironmentVariable(keyVal.Key, keyVal.Value);
    }
}

After this, it will work, and your application will be able to property access the environment variables.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77