asp.netc#-4.0gmailurlencodehtml-encode

How to encode the plus (+) symbol in a URL


The URL link below will open a new Google mail window. The problem I have is that Google replaces all the plus (+) signs in the email body with blank space. It looks like it only happens with the + sign. How can I remedy this? (I am working on a ASP.NET web page.)

https://mail.google.com/mail?view=cm&tf=0&to=someemail@somedomain.com&su=some subject&body=Hi there+Hello there

(In the body email, "Hi there+Hello there" will show up as "Hi there Hello there")


Solution

  • The + character has a special meaning in [the query segment of] a URL => it means whitespace: . If you want to use the literal + sign there, you need to URL encode it to %2b:

    body=Hi+there%2bHello+there
    

    Here's an example of how you could properly generate URLs in .NET:

    var uriBuilder = new UriBuilder("https://mail.google.com/mail");
    
    var values = HttpUtility.ParseQueryString(string.Empty);
    values["view"] = "cm";
    values["tf"] = "0";
    values["to"] = "someemail@somedomain.com";
    values["su"] = "some subject";
    values["body"] = "Hi there+Hello there";
    
    uriBuilder.Query = values.ToString();
    
    Console.WriteLine(uriBuilder.ToString());
    

    The result:

    https://mail.google.com:443/mail?view=cm&tf=0&to=someemail%40somedomain.com&su=some+subject&body=Hi+there%2bHello+there