0

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");
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
ToVo
  • 13
  • 1
  • 5
  • Remove *all* the UTF8 related code. .NET strings are UTF16 already. – Panagiotis Kanavos Aug 08 '19 at 14:22
  • That i have already attempt before i tried the UTF8 stuff. The encoding had made no diffenrence. – ToVo Aug 08 '19 at 14:31
  • *Remove* any attempts at encoding to begin with. It *does* make a difference. Maybe not with `\n` and `\r` as they are part of US-ASCII, but it will definitely mangle any non-English character and many special characters like `£`, `¦` or `¤`. – Panagiotis Kanavos Aug 08 '19 at 14:32
  • I was able to send multiline messages without any kind of encoding. The question's code doesn't use `publishRequest` though while `MessageAttributes` is a dictionary, not a string – Panagiotis Kanavos Aug 08 '19 at 15:28
  • yes you are right, my mistake. I had paste the wrong publish-method. Sorry – ToVo Aug 08 '19 at 15:49

1 Answers1

0

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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236