Search code examples
c#.net-coreamazon-snsaws-sdk-net

How can i create correct line breaks in a sns sms message?


I'm using the AWS .NET-SDK for sending SMS messages with the AWS SNS service. So far, so good; but when I use line breaks, I see the ? char at this point before the line break begins in the SMS. After that character, the line break is added as expected. Is there any possibility to get a line break without this ? character?

I have also tried following:

  • StringBuilder.AppendLine,
  • "\\n",
  • "\\r\\n",
  • @"\n",
  • @"\r\n",
  • Environment.NewLine

And encoding the string into UTF-8.

Example which doesn't work:

// Create message string
var sb = new StringBuilder();
sb.AppendLine("Line1.");
sb.Append("Line2.\\n");
sb.AppendLine(Environment.NewLine);
sb.Append(@"Line4\n");

// Encode into UTF-8
var utf8 = UTF8Encoding.UTF8;
var stringBytes = Encoding.Default.GetBytes(sb.ToString());
var decodedString = utf8.GetString(stringBytes);
var message = decodedString;

// Create request
var publishRequest = new PublishRequest
{
    PhoneNumber = "+491234567890",
    Message = message,
    Subject = "subject",
    MessageAttributes = "Promotional"
};

// Send SMS
var response = await snsClient.PublishAsync("topic", message, "subject");

Solution

  • Simply remove all attempts to encode the string. .NET strings are Unicode, specifically UTF16 already. PublishAsync expects a .NET string, not UTF8 bytes.

    As for why this error occurs, it's because the code converts the string into bytes using the local machine's codepage and then tries to read those bytes as if they were UTF8, which they aren't - using UTF8 as a system codepage is a beta feature on Windows 10 which breaks a lot of applications.

    The newline character for SMS is \n. Environment.NewLine returns \r\n unless you use .NET Core on Linux. StringBuilder.AppendLine uses Environment.NewLine so you can't use it.

    You shouldn't need anything more than String.Join to combine multiple lines into a single message:

    var message=String.Join("\n",lines);
    

    If you need to use a StringBuilder, use AppendFormat to append a line with the \n character at the end, eg :

    builder.AppendFormat("{0}\n",line);
    

    Update

    I was able to send an SMS containing newlines with this code:

    var region = Amazon.RegionEndpoint.EUWest1;
    var  snsClient = new AmazonSimpleNotificationServiceClient(region);
    
    var sb = new StringBuilder()
                    .Append("Line1.\n")
                    .Append("Line2.\n")
                    .Append("Line4\n");
    var message = sb.ToString();
    
    // Create request
    var publishRequest = new PublishRequest
    {
        PhoneNumber = phone,
        Message = message,                
    };
    
    // Send SMS
    var response = await snsClient.PublishAsync(publishRequest);
    

    The message I received contained :

    Line1.
    Line2.
    Line4.
    

    I decided to get fancy and changed the last line to :

    .Append("Line4ΑΒΓ£§¶\n");
    

    I received this text without problems too