发送邮件以及短信的方法

private static void SendMessageToMail()
        {

            string strSendMail = "发送邮件的邮箱地址";
            string strPwd = "发送邮件的邮箱地址所对应的授权码";
            string strSendName = "发送者的名称";
            string strServerMailAddress = "邮箱的服务smtp.qq.com";
            if (string.IsNullOrEmpty(strSendMail))
            {
                return;
            }
            try
            {
                string strSendTo = "这个邮件的接收人";
                if (string.IsNullOrWhiteSpace(strSendTo))
                {
                    return;
                }
                MailAddress sendfrom = new MailAddress(strSendMail, strSendName);
                MailAddress sendto = new MailAddress(strSendTo);
                MailMessage message = new MailMessage(sendfrom, sendto);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = "邮件标题";
                message.IsBodyHtml = true;
                message.Body = "
" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
邮箱用户为:" + strSendTo + ",您好:
消息体。
参数是XXXX " + "
"; SmtpClient sc = new SmtpClient(strServerMailAddress);//发送邮件用到的smtp主机(用不同的邮箱,stmp定义则不同) sc.Credentials = new System.Net.NetworkCredential(strSendMail, strPwd);//邮箱账号与密码 sc.Send(message); //发送邮件 } catch { } }

private static void SendMessageToPhone()
        {
            string Uid = "发送消息的注册号";
            string key = "授权码";
            string url = "http://utf8.api.smschinese.cn/";
            if (string.IsNullOrEmpty(Uid) || string.IsNullOrEmpty(key))
            {
                return;
            }
           
            string smsMob = "接收短信的手机号";
            if (string.IsNullOrWhiteSpace(smsMob))
            {
                continue;
            }
            string msg = string.Format("消息内容为XXXX");
            string smsText = HttpUtility.UrlEncode(msg);
            string post = string.Format("Uid={0}&key={1}&smsMob={2}&smsText={3}", Uid, key, smsMob, smsText);
            postData(url, post);
               
        }

        public static string postData(string url, string jsonStr = "", int timeout = 5000)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                Encoding encoding = Encoding.UTF8;
                byte[] bs = Encoding.UTF8.GetBytes(jsonStr);
                string responseData = String.Empty;
                req.Method = "POST";

                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = bs.Length;
                req.Timeout = timeout;

                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                    reqStream.Close();
                }

                using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        responseData = reader.ReadToEnd().ToString();
                    }
                    return responseData;
                }
            }
            catch (Exception ex)
            {
                return null;
                //throw ex;
            }
        }

你可能感兴趣的:(html,c#)