C#使用Azure和Fire& Forget发送电子邮件或SMS异步

时间:2014-08-02 16:57:43

标签: c# azure azure-queues

我正在寻找一种简单的方法来解雇和忘记Twilio SMS或电子邮件,我认为Azure Queue或Bus正是如此。但后来我开始阅读每一个,似乎它们的目的是做一些更复杂的事情。

问题:
什么是实施" Fire& Forget"的最佳方法。方法/电话,以便我知道我发送了一封电子邮件,但我的申请不需要等待消息实际离开。

1 个答案:

答案 0 :(得分:2)

这是一个使用SendGrid发送邮件的同步版本。您可以非常轻松地将其转换为异步。

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);
    }

来自here的一个简单的异步EAP样式版本:

 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";
        }
    }

来自here的第三个TAP风格:

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);

对于Twilio SMS API,您可以从教程here开始。完全异步支持。