예제 #1
0
        public static Task <SendResult> SendMail(SendCloudMessage mailMessage, Credential credential)
        {
            if (null == mailMessage || null == credential)
            {
                throw new ArgumentNullException("mailMessage or credential can't be null!");
            }

            var formData = new Dictionary <string, string>();

            formData.Add(IdParaName, credential.Id);
            formData.Add(KeyParaName, credential.Key);

            if (mailMessage.Parameters.Count > 0)
            {
                formData.Add(XsmtpapiParaName, BuildXsmtpJson(mailMessage.Parameters, mailMessage.To));
            }
            formData.Add(SubjectParaName, mailMessage.Subject);
            formData.Add(HtmlParaName, mailMessage.Html);
            formData.Add(FromParaName, mailMessage.From.ToString());
            formData.Add(ToParaName, BuildAddressList(mailMessage.To));

            try
            {
                var resultJson = HttpService.HttpPost(ApiUrl, formData, Encoding.UTF8);
                var jObject    = JObject.Parse(resultJson);
                var result     = jObject.ToObject <SendCloudResult>();
                return(Task.FromResult <SendResult>(new SendResult()
                {
                    Successful = result.result,
                    ErrorMessage = result.message,
                    Result = result.message
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult <SendResult>(new SendResult()
                {
                    Successful = false,
                    ErrorMessage = ex.Message
                }));
            }
        }
예제 #2
0
        public static Task <SendResult> SendMail(string subject, string html, string from, string to, Credential credential)
        {
            if (null == credential)
            {
                throw new ArgumentNullException("credential can't be null!");
            }

            var message = new SendCloudMessage()
            {
                Subject = subject,
                Html    = html,
                From    = new MailAddress(from),
                To      = new List <MailAddress>()
                {
                    new MailAddress(to)
                }
            };

            return(SendMail(message, credential));
        }