0

I have an application and want to use Twilio-SendGrid to send out some emails. I have looked a their c# suggestion, and created a basic DLL/COM file which works fine, but in addition to their example, I also want to return the "StatusCode" back to the Access application.

I need to establish, if the proposed static variable approach that I am using is a good way to go, or if I should use a different method.

    using System.Threading.Tasks;
    using SendGrid;
    using SendGrid.Helpers.Mail;

    namespace TSLib_SendGrid
    {
        public class SendGrid
        {
            public static string strResponse = "";

            public string SendNow(string apiKey)
            {
                Execute(apiKey).Wait();
                return strResponse;
            }   

            static async Task<string> Execute(string apiKey)
            {
                var client = new SendGridClient(apiKey);
                var from = new EmailAddress("abc@123.com", "From user");
                var to = new EmailAddress("xyz@987.com", "To recipient");
                var subject = "Sending with SendGrid is Fun";
                var plainTextContent = "and easy to do anywhere, even with C#";
                var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
                var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
                var response = await client.SendEmailAsync(msg);

                strResponse = response.StatusCode.ToString();
                return strResponse;
           }
        }
    }
Andy
  • 91
  • 1
  • 12
  • 1
    Neither COM nor VBA know about Task or async await. If possible, the VBA macro should access the `Result` property of the returned task, or call the `Wait()` method, to block until the task finishes. It's probably better to turn `Execute` itself into a synchronous method, by using `.Result` to retrieve `SendEmailAsync`'s result – Panagiotis Kanavos Feb 24 '20 at 11:00
  • Another option would be to raise events from your `SendGrid` class, as shown [in this question](https://stackoverflow.com/questions/39511528/exposing-net-events-to-com). – Panagiotis Kanavos Feb 24 '20 at 11:06
  • Thanks for that - but I have noticed that when the VBA function is called, which in-turn calls the COM object, it doesn't give me the response until everything has finished correctly. As for the other link, I'll take a look. – Andy Feb 24 '20 at 11:38
  • What's the question then? What does the VBA code look like? Did you try to read the `Result` property? – Panagiotis Kanavos Feb 24 '20 at 11:41
  • The question was about how I use the variable, and if I am doing things correctly (I'm still learning .Net). The .Net application creates the DLL/COM file, Access/VBA References it and initiates the email. Ultimately, I just want to get the StatusCode back into the VBA function. What I have works, but is it an ok way to do it? – Andy Feb 24 '20 at 13:19
  • It's unclear what you're asking. If it works, then what are the things you worry about? – Erik A Feb 24 '20 at 13:25
  • My apologies if it's unclear - I'm just trying to make sure that what I am doing (in .Net), is technically a good way to do it. Hence the question, "I need to establish, if the proposed static variable approach that I am using is a good way to go, or if I should use a different method." – Andy Feb 24 '20 at 13:31
  • 1
    That should be fine. thus your VBA will be: - strResult = Myobj.SendNow("apiKey") – Albert D. Kallal Feb 25 '20 at 01:12

0 Answers0