Exemplo n.º 1
0
        public async Task <IServiceResult> SendAsync(SMSModel sms)
        {
            Log.Debug($"ParsGreenSMSService.SendAsync.Begin => {sms}");

            try {
                //var response = await new SendSMSSoapClient(new SendSMSSoapClient.EndpointConfiguration() { })
                //    .SendAsync(_appSetting.SMSConfig.Signature, sms.PhoneNo, sms.TextBody, string.Empty);

                #region get
                var client  = new RestClient("http://login.parsgreen.com/");
                var request = new RestRequest("UrlService/sendSMS.ashx", Method.GET);

                request.AddParameter("from", _appSetting.SMSConfig.Number);
                request.AddParameter("to", sms.PhoneNo);
                request.AddParameter("text", sms.TextBody);
                request.AddParameter("signature", "");

                var response = await client.ExecuteAsync <string>(request);

                Log.Debug($"ParsGreenSMSService.SendAsync.End => {response}");

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    if (response.Data == string.Empty)
                    {
                        return(DataTransferer.Ok());
                    }
                }
                #endregion

                #region post
                //var client = new RestClient("http://login.parsgreen.com/");
                //var request = new RestRequest("UrlService/sendSMS.ashx", Method.POST);

                //var bodyparams = new Dictionary<string, string> {
                //    ["from"] = "",
                //    ["to"] = sms.PhoneNo,
                //    ["text"] = sms.TextBody,
                //    ["signature"] = "",
                //};
                //request.AddBody(bodyparams);

                //var response = client.Execute<string>(request);
                #endregion
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
            }

            return(DataTransferer.InternalServerError());
        }
Exemplo n.º 2
0
        public async Task <IServiceResult> SendAsync(EmailModel email)
        {
            try {
                if (string.IsNullOrWhiteSpace(email.Address) || string.IsNullOrWhiteSpace(email.Subject) ||
                    string.IsNullOrWhiteSpace(email.Body))
                {
                    return(DataTransferer.DefectiveEntry());
                }

                var mail = new MailMessage()
                {
                    From       = new MailAddress(_appSetting.SmtpConfig.Address),
                    Subject    = email.Subject,
                    Body       = email.Body,
                    IsBodyHtml = email.IsBodyHtml,
                    Priority   = MailPriority.High
                };
                mail.To.Add(new MailAddress(email.Address));
                if (!string.IsNullOrWhiteSpace(email.AttachmentPath) && File.Exists(email.AttachmentPath))
                {
                    mail.Attachments.Add(new Attachment(email.AttachmentPath));
                }

                using (var smtp = new SmtpClient(_appSetting.SmtpConfig.Host, _appSetting.SmtpConfig.Port)) {
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials           = new NetworkCredential(_appSetting.SmtpConfig.Username, _appSetting.SmtpConfig.Password);
                    smtp.EnableSsl             = true;
                    //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    await smtp.SendMailAsync(mail);
                }

                return(DataTransferer.Ok());
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(DataTransferer.InternalServerError());
            }
        }