Ejemplo n.º 1
0
        public static void SendWelcomeMail(string userName,string email)
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));
            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", "superjokes_register"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", "{\"to\": [\"" + email + "\"], \"sub\" : { \"%username%\" : [\"" + userName + "\"]}}"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", "superjokes_cn"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", AppConfig.SendCloudKey));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", "*****@*****.**"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", "超级冷笑话"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", "超级冷笑话注册邮件"));
            CodeScales.Http.Methods.HttpResponse response = client.Execute(postMethod);

            var repCode = response.ResponseCode;
            var repResult = EntityUtils.ToString(response.Entity);
            //LogHelper.Info("reg:" + email + repResult + AppConfig.SendCloudKey);

            //Console.WriteLine("Response Code: " + response.ResponseCode);
            //Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity));

            //Response.Write("Response Code: " + response.ResponseCode);
            //Response.Write("<br/>");
            //Response.Write("Response Content: " + EntityUtils.ToString(response.Entity));
        }
        public void HttpPostWithFilesAndParameters()
        {
            // this method sends an empty file (or no file :)
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200));

            MultipartEntity multipartEntity = new MultipartEntity();
            StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1");
            multipartEntity.AddBody(stringBody1);
            StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>");
            multipartEntity.AddBody(stringBody2);
            FileBody fileBody1 = new FileBody("photo", "me.file", null);
            multipartEntity.AddBody(fileBody1);
            postMethod.Entity = multipartEntity;
            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            MessageData md = new MessageData();
            md.PostParameters.Add(new NameValuePair("param1", "value1"));
            md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>"));
            md.Files.Add(new NameValuePair("me.file", "0"));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
Ejemplo n.º 3
0
        public static void VerifyNotice(string userName,string email,string jokeTitle,string jokeurl)
        {
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));
            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", "superjokes_verifynotice"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", "{\"to\": [\"" + email + "\"], \"sub\" : { \"%username%\" : [\"" + userName + "\"],\"%joketitle%\":[\"" + jokeTitle + "\"],\"%jokeurl%\":[\"" + jokeurl + "\"]}}"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", "superjokes_cn"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", AppConfig.SendCloudKey));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", "*****@*****.**"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", "超级冷笑话"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", "超级冷笑话审核通知"));
            CodeScales.Http.Methods.HttpResponse response = client.Execute(postMethod);

            var repCode = response.ResponseCode;
            var repResult = EntityUtils.ToString(response.Entity);
            //LogHelper.Info("verify:" + email + repResult + AppConfig.SendCloudKey);
        }
Ejemplo n.º 4
0
        public static bool SendSMS(SendCloud sc)
        {
            try
            {
                string sVar = GetVarJson(sc.listVar[0]);

                StringBuilder sb = new StringBuilder();
                sb.Append(sc.API_Key).Append("&");
                sb.Append("phone=").Append(sc.To).Append("&");
                sb.Append("smsUser="******"&");
                sb.Append("templateId=").Append(sc.Template).Append("&");
                sb.Append("vars=").Append(sVar).Append("&");
                sb.Append(sc.API_Key);
                string sSignature = Encryption.UserMd5(sb.ToString());

                HttpClient client = new HttpClient();
                HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/smsapi/send"));

                MultipartEntity multipartEntity = new MultipartEntity();
                postMethod.Entity = multipartEntity;

                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "smsUser", sc.API_User));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "signature", sSignature));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "templateId", sc.Template));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "phone", sc.To));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "vars", sVar));

                HttpResponse response = client.Execute(postMethod);

                if (response.ResponseCode == 200)
                {
                    string entity = EntityUtils.ToString(response.Entity);
                    Hashtable ht = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Hashtable>(entity);
                    if ((string)ht["message"] == "请求成功" && (bool)ht["result"])
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }
Ejemplo n.º 5
0
        public static string SendEmailByList(SendCloud sc)
        {
            try
            {
                HttpClient client = new HttpClient();
                HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));

                MultipartEntity multipartEntity = new MultipartEntity();
                postMethod.Entity = multipartEntity;

                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", sc.API_User));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", sc.API_Key));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", sc.From));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", sc.FromName));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", string.Join(" ", sc.listSubject.ToArray())));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", sc.Template));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "use_maillist", "true"));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "to", sc.To));
                multipartEntity.AddBody(new StringBody(Encoding.UTF8, "label", sc.Label));

                HttpResponse response = client.Execute(postMethod);

                return response.ResponseCode.ToString();
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
Ejemplo n.º 6
0
        public static bool SendEmail(SendCloud sc)
        {
            bool result = false;
            int iPageSize = 100;
            int iPageCount = sc.listVar.Count / iPageSize + (sc.listVar.Count % iPageSize == 0 ? 0 : 1);
            for (int i = 0; i < iPageCount; i++)
            {
                try
                {
                    string sVar = GetVarJson(sc.listVar.GetRange(i * iPageSize, i == iPageCount - 1 ? sc.listVar.Count - iPageSize * i : iPageSize));
                    sVar = sVar.Replace("\r", "").Replace("\n", "");

                    HttpClient client = new HttpClient();
                    HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.json"));

                    MultipartEntity multipartEntity = new MultipartEntity();
                    postMethod.Entity = multipartEntity;

                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", sc.API_User));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", sc.API_Key));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", sc.From));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", sc.FromName));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", string.Join(" ", sc.listSubject.ToArray())));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", sc.Template));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "label", sc.Label));
                    multipartEntity.AddBody(new StringBody(Encoding.UTF8, "substitution_vars", sVar));

                    HttpResponse response = client.Execute(postMethod);
                    //result += sVar + Environment.NewLine;

                    if (response.ResponseCode == 200)
                        result = true;
                    //{
                    //    result += EntityUtils.ToString(response.Entity) + Environment.NewLine + "==============" + Environment.NewLine;
                    //}
                    //else
                    //    result += response.ResponseCode.ToString() + Environment.NewLine + "==============" + Environment.NewLine;
                }
                catch (Exception ex)
                {
                    //return ex.ToString();
                }
            }
            return result;
        }
Ejemplo n.º 7
0
 private void AttachFormParams(ISendGrid message, MultipartEntity multipartEntity)
 {
     var formParams = FetchFormParams(message);
     formParams.ForEach(kvp => multipartEntity.AddBody(new StringBody(Encoding.UTF8, kvp.Key, kvp.Value)));
 }
Ejemplo n.º 8
0
 private void AttachFiles(ISendGrid message, MultipartEntity multipartEntity)
 {
     var files = FetchFileBodies(message);
     files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files[" + kvp.Key + "]", kvp.Key, kvp.Value)));
 }
Ejemplo n.º 9
0
        internal HttpClient InitializeTransport(out MultipartEntity multipartEntity, out HttpPost postMethod)
        {
            var client = new HttpClient();
            postMethod = new HttpPost(new Uri(_restEndpoint));

            multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            return client;
        }
        public void HttpPostWithFilesAndParameters2()
        {
            // this method sends a file and checks for the number of bytes recieved
            HttpClient client = new HttpClient();
            HttpPost postMethod = new HttpPost(new Uri(Constants.HTTP_MULTIPART_POST_200));

            MultipartEntity multipartEntity = new MultipartEntity();

            string fileName = "big-text.txt";

            FileInfo fi = ResourceManager.GetResourceFileInfo(fileName);
            FileBody fileBody1 = new FileBody("file", fileName, fi, "text/plain");

            multipartEntity.AddBody(fileBody1);
            postMethod.Entity = multipartEntity;

            StringBody stringBody1 = new StringBody(Encoding.ASCII, "param1", "value1");
            multipartEntity.AddBody(stringBody1);
            StringBody stringBody2 = new StringBody(Encoding.ASCII, "param2", "!#$^&*((<>");
            multipartEntity.AddBody(stringBody2);

            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            MessageData md = new MessageData();
            md.PostParameters.Add(new NameValuePair("param1", "value1"));
            md.PostParameters.Add(new NameValuePair("param2", "!#$^&*((<>"));
            md.Files.Add(new NameValuePair(fileName, fi.Length.ToString()));
            Assert.AreEqual(md.ToString(), responseString);
            Assert.AreEqual(Constants.HTTP_MULTIPART_POST_200, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
        public void HttpPostWithFilesAndParameters3()
        {
            // this method sends a file and checks for the number of bytes recieved
            HttpClient client = new HttpClient();
            string url = "http://www.fiddler2.com/sandbox/FileForm.asp";
            HttpPost postMethod = new HttpPost(new Uri(url));

            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;

            string fileName = "small-text.txt";

            StringBody stringBody1 = new StringBody(Encoding.ASCII, "1", "1_");
            multipartEntity.AddBody(stringBody1);

            FileInfo fi = ResourceManager.GetResourceFileInfo(fileName);
            FileBody fileBody1 = new FileBody("fileentry", fileName, fi, "text/plain");
            multipartEntity.AddBody(fileBody1);

            StringBody stringBody2 = new StringBody(Encoding.ASCII, "_charset_", "windows-1252");
            multipartEntity.AddBody(stringBody2);

            HttpResponse response = client.Execute(postMethod);

            Assert.AreEqual(200, response.ResponseCode);
            string responseString = EntityUtils.ToString(response.Entity);
            Assert.AreEqual(url, response.RequestUri.AbsoluteUri);
            Console.Write(responseString);
        }
Ejemplo n.º 12
0
        private void AttachFiles(ISendGrid message, MultipartEntity multipartEntity)
        {
            var files = FetchFileBodies(message);
            files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files[" + Path.GetFileName(kvp.Key) + "]", Path.GetFileName(kvp.Key), kvp.Value)));

            var streamingFiles = FetchStreamingFileBodies(message);
            streamingFiles.ForEach(kvp => multipartEntity.AddBody(new StreamedFileBody(kvp.Value, kvp.Key)));
        }