Sample code for 30+ languages & platforms
Visual Basic 6.0 Requires Chilkat v11.6.0+

Amazon SNS: Send Korean, Chinese, or Japanese SMS from a UTF-8 File

See more Amazon SNS Examples

Uses the current Amazon SNS Query API (Version=2010-03-31) through Chilkat.Rest and AuthAws, with HTTPS, Signature Version 4, and service sns. The API version date remains current. Action and input parameters are URL-encoded into a POST body, avoiding message text and recipient addresses in the URL. Set the actual region once; other AWS partitions need their documented endpoint suffix and ARN partition.

Load private qa_data/aws/sns.json with nonempty string fields access_key, secret_key, and session_token when using temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles, obtain credentials, or refresh them automatically. The session token is included in the signed form body on every request. A Socket explicitly enables TLS certificate verification before passing the connection to Rest.

Checks setup calls, transport completion, HTTP status, XML parsing, result root, and required fields. Normal output includes identifiers, not full messages or headers. Keep detailed diagnostics private. For retryable failures, use bounded backoff with jitter in the application; these examples do not automatically retry writes. A Publish timeout can leave an accepted message and retrying a standard-topic/SMS publish can duplicate it. A returned MessageId confirms SNS acceptance, not subscriber or handset delivery.

Use an authorized recipient in E.164 format. New SMS accounts start in the regional SMS sandbox and can send only to verified destinations. Origination identities, country support, registration, spending limits and opt-out status can affect delivery. Confirm the appropriate configuration with SNS SMS sandbox guidance and SNS SMS sending guidance.

Messages can be segmented and billed as multiple SMS messages; encoding affects segment size. The sample chooses AWS.SNS.SMS.SMSType=Transactional for a transactional test message; choose a type appropriate to the actual use. A MessageId is not a delivery receipt. Direct SMS uses PhoneNumber without TopicArn or TargetArn. There is no need to create a topic or subscription for a direct send.

Reads qa_data/txt/hello_korean_chinese_japanese.txt as UTF-8 and checks LoadFile before sending. Unicode characters are form-encoded exactly once; no manual URL escaping is needed. The local character check is an input bound, not a GSM/UCS-2 segment or cost estimator. SNS and downstream providers determine encoding eligibility and segmentation. Verbose logging is disabled and message text is not printed.

Required IAM action: sns:Publish. Grant only the intended resource access; direct SMS does not have a topic ARN resource. Topic policies and KMS key access can also affect requests.

AWS Publish reference

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

Dim stringCompare As New ChilkatStringBuilder
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Private credentials; do not put this file in public web content or source control.
Dim credentials As New ChilkatJsonObject
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/sns.json")
If (success <> 1) Then
    Debug.Print "Could not load private SNS credentials."
    Exit Sub
End If

success = stringCompare.SetString(credentials.StringOf("access_key"))
Dim stringMatch1 As Long
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = 1)) Then
    Debug.Print "Missing or invalid access_key."
    Exit Sub
End If

success = stringCompare.SetString(credentials.StringOf("secret_key"))
Dim stringMatch2 As Long
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = 1)) Then
    Debug.Print "Missing or invalid secret_key."
    Exit Sub
End If

Dim region As String
region = "us-west-2"
Dim endpoint As New ChilkatStringBuilder
success = endpoint.Append("sns.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
Dim aws As New ChilkatAuthAws
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "sns"
aws.SignatureVersion = 4
Dim rest As New ChilkatRest
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
success = rest.SetAuthAws(aws)
If (success <> 1) Then
    Debug.Print "Could not configure AWS signing."
    Exit Sub
End If

If (credentials.HasMember("session_token") = 1) Then
    success = stringCompare.SetString(credentials.StringOf("session_token"))
    Dim stringMatch3 As Long
    stringMatch3 = stringCompare.ContentsEqual("",1)
    If ((credentials.JsonTypeOf("session_token") <> 1) Or (stringMatch3 = 1)) Then
        Debug.Print "session_token must be a nonempty string when supplied."
        Exit Sub
    End If

End If

Dim xml As New ChilkatXml
xml.Strict = 1
Dim value As String
'  Use an authorized recipient in E.164 format; this number is a placeholder.
Dim phoneNumber As String
phoneNumber = "+15551234567"
Dim phone As New ChilkatStringBuilder
success = phone.Append(phoneNumber)
success = stringCompare.SetString(phone.GetRange(1,1,0))
Dim stringMatch4 As Long
stringMatch4 = stringCompare.ContentsEqual("0",1)
If ((phone.StartsWith("+",1) <> 1) Or (phone.Length < 2) Or (phone.Length > 16) Or (stringMatch4 = 1)) Then
    Debug.Print "Use E.164: + followed by at most 15 digits, starting with a nonzero digit."
    Exit Sub
End If

Dim digits As New ChilkatStringBuilder
success = digits.Append("0123456789")
Dim position As Long
position = 1
Do While (position < phone.Length)
    If (digits.Contains(phone.GetRange(position,1,0),1) <> 1) Then
        Debug.Print "Phone number must contain only + and digits."
        Exit Sub
    End If

    position = position + 1
Loop
Dim message As New ChilkatStringBuilder
success = message.LoadFile("qa_data/txt/hello_korean_chinese_japanese.txt","utf-8")
If (success <> 1) Then
    Debug.Print "Could not read the UTF-8 message file."
    Exit Sub
End If

If ((message.Length = 0) Or (message.Length > 1600)) Then
    Debug.Print "Use a nonempty SMS message within the 1600-character Publish limit; AWS applies encoding and segmentation rules."
    Exit Sub
End If

'  Explicit certificate verification; keep this Socket alive while Rest uses it.
Dim connection As New ChilkatSocket
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
    Debug.Print "TLS connection failed; inspect diagnostics privately."
    Exit Sub
End If

success = rest.UseConnection(connection,1)
If (success <> 1) Then
    Debug.Print "Could not attach TLS connection."
    Exit Sub
End If

success = rest.ClearAllQueryParams()
If (success <> 1) Then
    Debug.Print "Could not clear parameters from the previous request."
    Exit Sub
End If

'  Build a fresh form for each request; do not retain previous parameters.
'  AddQueryParam encodes raw values into the POST body. Do not pre-encode them.
success = rest.AddQueryParam("Action","Publish")
If (success <> 1) Then
    Debug.Print "Could not set Action."
    Exit Sub
End If

success = rest.AddQueryParam("Version","2010-03-31")
If (success <> 1) Then
    Debug.Print "Could not set Version."
    Exit Sub
End If

'  Include temporary credentials in the signed form body on every request.
If (credentials.HasMember("session_token") = 1) Then
    success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
    If (success <> 1) Then
        Debug.Print "Could not set X-Amz-Security-Token."
        Exit Sub
    End If

End If

success = rest.AddQueryParam("PhoneNumber",phoneNumber)
If (success <> 1) Then
    Debug.Print "Could not set PhoneNumber."
    Exit Sub
End If

success = rest.AddQueryParamSb("Message",message)
If (success <> 1) Then
    Debug.Print "Could not set SMS message."
    Exit Sub
End If

success = rest.AddQueryParam("MessageAttributes.entry.1.Name","AWS.SNS.SMS.SMSType")
If (success <> 1) Then
    Debug.Print "Could not set MessageAttributes.entry.1.Name."
    Exit Sub
End If

success = rest.AddQueryParam("MessageAttributes.entry.1.Value.DataType","String")
If (success <> 1) Then
    Debug.Print "Could not set MessageAttributes.entry.1.Value.DataType."
    Exit Sub
End If

success = rest.AddQueryParam("MessageAttributes.entry.1.Value.StringValue","Transactional")
If (success <> 1) Then
    Debug.Print "Could not set MessageAttributes.entry.1.Value.StringValue."
    Exit Sub
End If

Dim responseXml As String
responseXml = rest.FullRequestFormUrlEncoded("POST","/")
If (rest.LastMethodSuccess <> 1) Then
    Debug.Print "Request did not complete. Its outcome may be uncertain; reconcile before retrying."
    Exit Sub
End If

success = xml.LoadXml(responseXml)
If (rest.ResponseStatusCode <> 200) Then
    Debug.Print "SNS HTTP status: " & rest.ResponseStatusCode
    If (success = 1) Then
        value = xml.GetChildContent("Error|Code")
        If (xml.LastMethodSuccess = 1) Then
            Debug.Print "AWS error code: " & value
        End If

        value = xml.GetChildContent("RequestId")
        If (xml.LastMethodSuccess = 1) Then
            Debug.Print "AWS request ID: " & value
        End If

    End If

    '  Full messages, headers, and signing diagnostics may contain private data.
    Exit Sub
End If

If (success <> 1) Then
    Debug.Print "Expected a well-formed XML response."
    Exit Sub
End If

success = stringCompare.SetString(xml.Tag)
Dim stringMatch5 As Long
stringMatch5 = stringCompare.ContentsEqual("PublishResponse",1)
If (stringMatch5 <> 1) Then
    Debug.Print "Unexpected SNS response element."
    Exit Sub
End If

value = xml.GetChildContent("ResponseMetadata|RequestId")
success = stringCompare.SetString(value)
Dim stringMatch6 As Long
stringMatch6 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch6 = 1)) Then
    Debug.Print "Missing or empty ResponseMetadata|RequestId."
    Exit Sub
End If

Debug.Print "AWS request ID: " & value
value = xml.GetChildContent("PublishResult|MessageId")
success = stringCompare.SetString(value)
Dim stringMatch7 As Long
stringMatch7 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch7 = 1)) Then
    Debug.Print "Missing or empty PublishResult|MessageId."
    Exit Sub
End If

Debug.Print "Message ID: " & value
Debug.Print "SMS accepted by SNS; handset delivery is not confirmed."