3

Twilio SMS または電子メールのいずれかを起動して忘れる簡単な方法を探していますが、Azure Queue または Bus がまさにそれを行うと思いました。しかし、それぞれについて読み始めると、もっと複雑なことをするように設計されているようです。

質問:
「Fire&Forget」メソッド/呼び出しを実装して、電子メールを送信したことがわかるようにする最善の方法は何ですか?

4

1 に答える 1

2

SendGrid でメールを送信するための 1 つの同期バージョンを次に示します。非常に簡単に非同期に変換できます。

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

そして、ここから 1 つの単純な非同期 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";
        }
    }

そして、ここから 3 つ目の 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 については、こちらのチュートリアルから始めることができます。完全な非同期サポート。

于 2014-08-03T11:27:11.800 に答える