快跑啊兔兔 阅读(105) 评论(0)

兴趣是最好的老师。  --爱因斯坦

一、实现短信发送

1.使用短信mao的方式进行短信发送,前提要购买硬件设备,这里就不考虑展示了;

2.使用中国网建提供的短信平台,但是用几次后要收费;

我们这里主要介绍第二种方式实现短信发送,在网上(http://sms.webchinese.cn/)注册账号(注意密码不是短信密钥需自行获取),具体实现,可以参考SMS短信通API下行接口(http://sms.webchinese.cn/api.shtml),多种语言实现例子都有,笔者这里用C#实现

下面是代码demo

     private string strurl = "http://utf8.sms.webchinese.cn/?";
        private string strUid = "Uid=";
        private string strKey = "&Key=";
        private string strsmsMon = "&smsMob=";
        private string strsmsText = "&smsText=";
        public MainForm()
        {
            InitializeComponent();
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            if(txtSMSUserID.Text!=""&& txtTel.Text!="" && txtSMSUserID.Text != "")
            {
                string url = strurl + strUid + txtSMSUserID.Text + strKey + strsmsMon + txtTel.Text + strsmsText + txtContent.Text;
                string result= GetHtmlFromUrl(url);
                MessageBox.Show(result);
            }           
        }
        private string GetHtmlFromUrl(string url)
        {
            string targetUrl = url.Trim().ToString();
            string result = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                request.Method = "GET";
                request.Timeout = 30 * 60 * 1000;
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.Default);
                result = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.StackTrace);
            }
            return result;        
        }

二、实现邮件发送

. NET提供了很多很丰富的邮件发送与接收的API在System.Net.Mail命名空间里,使得实现邮件收发功能变得简单,下面是一个简单的邮件发送例子

下面是代码demo

     private string smtpService = "smtp.dnaerp.com";
        private string sendEmail = "Michael@dnaerp.com";
        private string sendPwd = "";

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            SmtpClient client = new SmtpClient(smtpService);
            MailAddress sendAddress = new MailAddress(sendEmail,"MichaelTest");
            MailAddress receiverAddress = new MailAddress(txtReceiver.Text);
            MailMessage message=new MailMessage(sendAddress, receiverAddress);
            message.Subject = "邮件demo测试";
            message.SubjectEncoding = Encoding.Default;
            message.Body = txtContent.Text;
            message.BodyEncoding = Encoding.Default;
            message.To.Add("");
            message.CC.Add("");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //是否启用ssl 安全发送
            client.EnableSsl = false;
            try
            {
                client.UseDefaultCredentials = false;
                NetworkCredential credential = new NetworkCredential(sendEmail,sendPwd);
                client.Credentials = credential;
                client.Send(message);
                MessageBox.Show("邮件发送成功");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.StackTrace);
            }
        }

 

关于实现发送短信,发送邮件的学习就到此结束了,希望能够帮到你,若有不足,欢迎斧正,感谢您的阅读。