1

I am using C# asp.net to take values passed from the query string and use them to send out an email with a mobile phone number so that the email will cause a text message to be sent to that phone. Ex. 9772565555@vtext.com. The email message arrives fine as a text message, but in the email body it cuts off the url at the end of the message. I'm certain I'm making a syntax error of some sort. Here's the code

    string phone = Request["phone"].ToString();
    string item = Request["item"].ToString();
            if (phone != null && phone != "")
            {
                try
                {
                    MailMessage mailMessage = new MailMessage();
                    mailMessage.To.Add(phone + "@vtext.com");
                    mailMessage.From = new MailAddress("no-reply@mydomain.com");
                    mailMessage.Subject = "Your Item"
                    mailMessage.Body = "Hello. Click the link http://mydomain/order.aspx?order=" + phone + item;
                    SmtpClient smtpClient = new SmtpClient("localhost", 25);
                    smtpClient.Send(mailMessage);
                    Response.Write("<div style='font-size:36px'>E-mail sent!</div>");
                }
                catch (Exception ex)
                {
                    Response.Write("<div style='font-size:36px'>Could not send the e-mail - error: " + ex.Message + "</div>");
                }
            }
  • Have you tried cc'ing to a normal email account to confirm the message body is "as expected"? – Rowland Shaw Jul 23 '15 at 14:56
  • 1
    What part of the URL is getting cut off? Vtext has a 160 character limit per their documentation. – ragerory Jul 23 '15 at 14:59
  • I think VText cuts off after 140 characters. – Icemanind Jul 23 '15 at 15:01
  • I tried it and I get the email and even though there is no html tags, the link text shows up and it's clickable and it works. Somehow, I think the ? in the url might be causing a problem where an android or iphone will not make it clickable. Either that or a weird syntax error – user4274335 Jul 23 '15 at 15:04
  • I'm at about 120 characters before the link. The interesting part is that it cuts it off right at the link, not halfway through it. So it says click the link, but nothing at all displays after that and what follows in nothing but a url, nothing else. So it doesn't like the url for some reason – user4274335 Jul 23 '15 at 15:07
  • I mean, it would be *great* if you could show the exact text message that gets sent. – ragerory Jul 23 '15 at 15:32
  • It's probably removing the URL because that is the string that is causing it to go over the character limit, rather than truncating it. – ragerory Jul 23 '15 at 16:12
  • I think you and Icemanind are right. I cut the message size down an it works. It was cutting off the whole url because there are no spaces in it, so even if half of it fell within the char limit, the whole thing gets thrown out. If someone wants to post that as an answer I can mark it – user4274335 Jul 23 '15 at 18:52

1 Answers1

0

In order to make it easier for anyone else going through this thread, here is the answer as per suggestions from ragerory and Icemanmind above.

Check that the final message length does not exceed the allowed character limit. In this case, roughly 120 characters. There's many options to handle this, but I shall mention two that might give you a head start.

(1) Stop the message from being sent (dirty method)

Const int maxLength = 120;


string messageText = "1234";

Boolean sendMessage = SendMessage(messageText, maxLength);
if (!sendMessage) MessageBox.Show("Could not send message. Character limit exceeded","Error");

Boolean SendMessage(string messageText, int messageCharLimit);
{
    if (messageText.Length() > messageCharLimit)
        return false;

    //TO DO - SEND MESSAGE
    return true;
}

(2) Break the message into chunks and send as multiple messages (extracted from the answer given by SLaks on Split String into smaller Strings by length variable)

public static IEnumerable<string> SplitByLength(this string str, int maxLength) {
for (int index = 0; index < str.Length; index += maxLength) {
    yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
  }
}
Community
  • 1
  • 1
TheDanMan
  • 1,746
  • 1
  • 17
  • 22