Esempio 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());
        }
Esempio n. 2
0
        public async Task <IServiceResult> SendAsync(EmailModel email)
        {
            var message = new MimeMessage();

            var from = new MailboxAddress("MailboxName", _appSetting.SmtpConfig.Host);

            message.From.Add(from);

            var to = new MailboxAddress(email.Address);

            message.To.Add(to);


            var bodyBuilder = new BodyBuilder();

            if (email.IsBodyHtml)
            {
                bodyBuilder.HtmlBody = email.Body;
            }
            else
            {
                bodyBuilder.TextBody = email.Body;
            }
            bodyBuilder.Attachments.Add(email.AttachmentPath);

            message.Subject = email.Subject;
            message.Body    = bodyBuilder.ToMessageBody();

            var client = new SmtpClient();

            client.Connect(_appSetting.SmtpConfig.Address, _appSetting.SmtpConfig.Port, true);
            client.Authenticate(_appSetting.SmtpConfig.Username, _appSetting.SmtpConfig.Password);

            await client.SendAsync(message);

            await client.DisconnectAsync(true);

            client.Dispose();

            return(DataTransferer.Ok());
        }
Esempio n. 3
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());
            }
        }