2019-11-20
2019-12-04 更新
WebForm 專案
先把 SnsComServer_v3.4.dll 加入參考。
Web.Config
<!-- 如需如何設定 ASP.NET 應用程式的詳細資訊,請前往 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <appsettings> <add key="SMSIP" value="簡訊主機IP1, 簡訊主機IP2," /> <add key="SMSPort" value="8001,8001" /> <add key="SMSAccount" value="帳號" /> <add key="SMSPassword" value="密碼" /> </appsettings> </configuration> |
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SMSTest.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="送出簡訊" /><br /> <asp:Label ID="Label_Message" runat="server"></asp:Label> </div> </form> </body> </html> |
Default.aspx.cs
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Security.Cryptography; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace SMSTest { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string phoneNumber = "0931xxxyyy"; // 手機號碼 try { RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider(); string smsip = (string)ConfigurationManager.AppSettings["SMSIP"]; string smsport = (string)ConfigurationManager.AppSettings["SMSPort"]; string account = (string)ConfigurationManager.AppSettings["SMSAccount"]; string passwd = (string)ConfigurationManager.AppSettings["SMSPassword"]; CHT.SNSCOMSERVER.SnsComObject3 m_sns = new CHT.SNSCOMSERVER.SnsComObject3(); m_sns.InitRemoteServer(smsip, smsport, account, passwd); int nResult = m_sns.Login(); if (nResult == 0) { //傳送手機號碼, 訊息內容 nResult = m_sns.SubmitMessage(null, phoneNumber, "簡訊測試", 1440); //若發訊成功,則更新至狀態查詢測試的兩個欄位,以便進一步查詢發送結果。 if (nResult == 0) { Label_Message.Text = "簡訊發送成功。"; } else { Label_Message.Text = "簡訊發送失敗。"; } } m_sns.Logout(); } catch (Exception ex) { if (ex != null) { Label_Message.Text = ex.Message.ToString(); } else { Label_Message.Text = "不明例外。"; } } } } } |
上述在 Visual Studio 2019 環境可正常執行,但 Deploy 到網站後會出錯。IIS 要增加設定才能解決。
(下圖) Click 圖片可看 100% 尺寸。
(完)
沒有留言:
張貼留言