public void Send(string to)
 {
     try
     {
         if (string.IsNullOrEmpty(From))
         {
             throw new Exception("'From' cannot be empty");
         }
         if (string.IsNullOrEmpty(to))
         {
             throw new ArgumentException(nameof(to));
         }
         var code = _verificationCode.Generate(out var result);
         var text = _parser.Parse(code);
         TwilioClient.Init(_configuration.AccountSid, _configuration.AuthToken);
         var message = MessageResource.Create(
             from: new PhoneNumber(From),
             to: new PhoneNumber(to),
             body: text
             );
         MessageSentHandler?.Invoke(result, message);
     }
     catch (Exception ex)
     {
         VerificationCodoeExceptionHandler?.Invoke(ex);
     }
 }
        public async void SendAsync <T>(Func <string, T> options) where T : class
        {
            try
            {
                if (options == null)
                {
                    throw new ArgumentException(nameof(options));
                }
                TwilioClient.Init(_configuration.AccountSid, _configuration.AuthToken);
                var code  = _verificationCode.Generate(out var result);
                var param = options.Invoke(code);
                if (!(param is CreateMessageOptions))
                {
                    return;
                }
                var msgOption = param as CreateMessageOptions;
                var message   = await MessageResource.CreateAsync(msgOption);

                MessageSentHandler?.Invoke(result, message);
            }
            catch (Exception ex)
            {
                VerificationCodoeExceptionHandler?.Invoke(ex);
            }
        }
        public byte[] Draw()
        {
            byte[] buffer;
            try
            {
                using (var ms = new MemoryStream())
                {
                    using (var image = DrawImage())
                    {
                        image.Save(ms, ImageFormat.Jpeg);
                        ms.Position = 0;
                        buffer      = new byte[ms.Length];
                        ms.Read(buffer, 0, Convert.ToInt32(ms.Length));
                        ms.Flush();
                    }
                }
            }
            catch (Exception ex)
            {
                VerificationCodoeExceptionHandler?.Invoke(ex);
                return(null);
            }

            return(buffer);
        }
Exemplo n.º 4
0
        public async void SendAsync(string to)
        {
            try
            {
                if (string.IsNullOrEmpty(From))
                {
                    throw new Exception("'From' cannot be empty");
                }
                if (string.IsNullOrEmpty(to))
                {
                    throw new ArgumentException(nameof(to));
                }
                var code    = _verificationCode.Generate(out var result);
                var text    = _parser.Parse(code);
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress(From));
                message.To.Add(new MailboxAddress(to));
                message.Body = new TextPart {
                    Text = text
                };
                using (var emailClient = new SmtpClient())
                {
                    await emailClient.ConnectAsync(_configuration.SmtpServer, _configuration.SmtpPort, _configuration.UseSsl);

                    if (AuthenticateHandlerDelegate != null)
                    {
                        AuthenticateHandlerDelegate.Invoke(emailClient);
                    }
                    else
                    {
                        if (_configuration.RemoveXOauth2)
                        {
                            emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
                        }
                        emailClient.Authenticate(_configuration.SmtpUsername, _configuration.SmtpPassword);
                    }

                    await emailClient.SendAsync(message);

                    MessageSentHandler?.Invoke(result, message);
                    emailClient.Disconnect(true);
                }
            }
            catch (Exception ex)
            {
                VerificationCodoeExceptionHandler?.Invoke(ex);
            }
        }
Exemplo n.º 5
0
        public async void SendAsync <T>(Func <string, T> options) where T : class
        {
            try
            {
                if (options == null)
                {
                    throw new ArgumentException(nameof(options));
                }
                var code  = _verificationCode.Generate(out var result);
                var param = options.Invoke(code);
                if (!(param is MimeMessage))
                {
                    return;
                }
                var message = param as MimeMessage;
                using (var emailClient = new SmtpClient())
                {
                    await emailClient.ConnectAsync(_configuration.SmtpServer, _configuration.SmtpPort,
                                                   _configuration.UseSsl);

                    if (AuthenticateHandlerDelegate != null)
                    {
                        AuthenticateHandlerDelegate.Invoke(emailClient);
                    }
                    else
                    {
                        if (_configuration.RemoveXOauth2)
                        {
                            emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
                        }
                        emailClient.Authenticate(_configuration.SmtpUsername, _configuration.SmtpPassword);
                    }

                    await emailClient.SendAsync(message);

                    MessageSentHandler?.Invoke(result, message);
                    emailClient.Disconnect(true);
                }
            }
            catch (Exception ex)
            {
                VerificationCodoeExceptionHandler?.Invoke(ex);
            }
        }