C # Send Email or SMS Async using Azure and Fire & Forget

I'm looking for an easy way to shoot and forget either SMS Twilio or email and I thought Azure Queue or Bus does just that. But then I start reading about each one, and it seems that they are meant to do something more complex.

Question:
What's the best way to implement the Fire & Forget method / call so that I know I sent an email, but my application does NOT need to stop waiting for the message to actually go away.

+3


source to share


1 answer


Here's one version of sync for sending mail using SendGrid. You can convert it to async easily.

public void SendMail(MailAddress @from, MailAddress[] to, string subject, AlternateView[] alternateMessageBodyViews, Attachment[] attachments)
    {
        _validationService.StringIsNullOrEmpty(subject, "subject");
        _validationService.Null(from, "from");
        _validationService.StringIsNullOrEmpty(from.Address, "from.Address");
        _validationService.Null(to, "to");


        /* Important Notice
         * SendGrid has 20480000 bytes limitation for the total message size. Approx. 19.5 MB
         * BUT attachments are Base64 encoded therefore 15MB of attachments will actually become 20 MB when encoded.
         * Therefore we need to cut down the attachments total ( cumulative ) size to 12 MB which will make 16 MB after base64
         * Combined with other message information like message headers, attachments headers, etc. will keep us away from the 19.5MB limit
         */
        long approximateTotalMessageMaxSizeInBytes = 0;

        MailMessage mailMsg = new MailMessage();
        // From
        mailMsg.From = new MailAddress(from.Address, from.DisplayName);

        approximateTotalMessageMaxSizeInBytes += Convert.ToBase64String(Encoding.UTF8.GetBytes(from.Address)).Length;
        approximateTotalMessageMaxSizeInBytes += Convert.ToBase64String(Encoding.UTF8.GetBytes(from.DisplayName)).Length;
        // To
        foreach (MailAddress toMailAddress in to)
        {
            mailMsg.To.Add(new MailAddress(toMailAddress.Address, toMailAddress.DisplayName));

            approximateTotalMessageMaxSizeInBytes += Convert.ToBase64String(Encoding.UTF8.GetBytes(toMailAddress.Address)).Length;
            approximateTotalMessageMaxSizeInBytes += Convert.ToBase64String(Encoding.UTF8.GetBytes(toMailAddress.DisplayName)).Length;
        }

        // Subject and multipart/alternative Body
        mailMsg.Subject = subject;
        approximateTotalMessageMaxSizeInBytes += Convert.ToBase64String(Encoding.UTF8.GetBytes(subject)).Length;

        if (alternateMessageBodyViews != null)
        {
            foreach (AlternateView alternateView in alternateMessageBodyViews)
            {
                mailMsg.AlternateViews.Add(alternateView);
                approximateTotalMessageMaxSizeInBytes += (long)(alternateView.ContentStream.Length * 1.33);
            }
        }

        if (attachments != null)
        {
            foreach (Attachment attachment in attachments)
            {
                mailMsg.Attachments.Add(attachment);
                approximateTotalMessageMaxSizeInBytes += (long)(attachment.ContentStream.Length * 1.33);
            }
        }


        if (approximateTotalMessageMaxSizeInBytes > long.Parse(ConfigurationManager.AppSettings["TotalMessageMaxSizeInBytes"]))
        {
            throw new MailMessageTooBigException(
                string.Format("Message total bytes limit is {0}. This message is approximately {1} bytes long.",
                    approximateTotalMessageMaxSizeInBytes));
        }


        try
        {
            _mailSender.Send(mailMsg);
        }
        catch(Exception exception) // swallow the exception so the server do not blow up because of a third party service.
        {
            _logger.Exception("SendGridMailService", _currentUserProvider.CurrentUserId, exception);
        }

        string logFrom = string.Format("{0} <{1}>", from.DisplayName, from.Address);
        string[] logTo = to.Select(t => string.Format("{0} <{1}>", t.DisplayName, t.Address)).ToArray();
        string[] atts = (attachments == null) ? null : attachments.Select(att => att.Name).ToArray();

        _logger.SendEmail(
            _currentUserProvider.CurrentSystemUser.UserName,
            logFrom,
            logTo,
            subject,
            atts);
    }

      

And one simple version of the async EAP style from here :

 public void SendAsyncMail()
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("Enter from mail address");
        mail.To.Add(new MailAddress("Enter to address #1"));
        mail.To.Add(new MailAddress("Enter to address #2"));
        mail.Subject = "Enter mail subject";
        mail.Body = "Enter mail body";

        SmtpClient smtpClient = new SmtpClient();
        Object state = mail;

        //event handler for asynchronous call
        smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
        try
        {
            smtpClient.SendAsync(mail, state);
        }
        catch (Exception ex)
        {

        }
    }
    void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {

        MailMessage mail = e.UserState as MailMessage;

        if (!e.Cancelled && e.Error != null)
        {
            //message.Text = "Mail sent successfully";
        }
    }

      



And the third TAP-style from here :

var message = new MailMessage("from", "to", "subject", "body"))
var client = new SmtpClient("host");
client.SendCompleted += (s, e) => {
                           client.Dispose();
                           message.Dispose();
                        };
client.SendAsync(message, null);

      

For the Twilio SMS API, you can get started with the tutorial right here . Full async support.

+2


source







All Articles