Imports System.Net
Imports System.Text
Imports Twilio

Public Class clsTwilioSMS

    Public Shared Function Send(ByVal pToNumber As String, ByVal pSMSMessage As String) As String
        Dim strReturn As String = "0"
        Dim strLog As String = Now.ToString("yyyy-MM-dd HH:mm:ss") & " Send Sms to " & pToNumber & vbLet

        Try
            Dim AccountSid As String = My.MySettings.Default.SMSAccountSid.ToString
            Dim AuthToken As String = My.MySettings.Default.SMSAuthToken.ToString
            Dim FromNumber As String = My.MySettings.Default.SMSFromNumber.ToString

            Dim Twilio As TwilioRestClient = New TwilioRestClient(AccountSid, AuthToken)
            Dim message As SMSMessage = Twilio.SendSmsMessage(FromNumber, pToNumber, pSMSMessage, "")
            If (message.Sid Is Nothing) Then
                strLog += " >>>Send Sms Failed , detail: " & message.RestException.ToString
                strReturn = "-1"
            Else
                strLog += " >>>Send Sms Success  , Sid: " & message.Sid & vbLet
                strReturn = "1"
            End If
        Catch ex As Exception
            strLog += " >>>Send Sms Exception , detail:" & ex.ToString & vbLet
        Finally
            LogByLine(strLog)
        End Try
        Return strReturn
    End Function

    Private Shared Sub LogByLine(ByVal pLine As String)
        Try
            If Trim(pLine) <> "" Then
                Dim p12File As String = "SMSLog.txt"
                Dim p12Filename As String = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/"), p12File)

                Dim aa As System.IO.StreamWriter = New System.IO.StreamWriter(p12Filename, True, System.Text.Encoding.UTF8)
                aa.WriteLine(pLine)
                aa.Close()
                aa.Dispose()
            End If
        Catch ex As Exception
        End Try
    End Sub




End Class

主要的Dll:

https://files.cnblogs.com/files/xiaobuild/TwilioDLL.zip

参考网站:

https://www.twilio.com/user/account/developer-tools/api-explorer/message-create

 

posted on 2015-06-18 14:50  QZB  阅读(275)  评论(0编辑  收藏  举报