C# 簡訊發送 郵件發送

來源:互聯網
上載者:User

標籤:oid   odi   suse   pcl   init   agent   eth   show   code   

興趣是最好的老師。  --愛因斯坦

一、實現簡訊發送

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 = "[email protected]";        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);            }        }

 

關於實現傳送簡訊,發送郵件的學習就到此結束了,希望能夠幫到你,若有不足,歡迎斧正,感謝您的閱讀。

 

C# 簡訊發送 郵件發送

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.