GMail-URLs generieren

private static String createGMailUrl( String to, String cc, String subject, String body )
{
  StringBuilder result = new StringBuilder( "https://mail.google.com/mail/?view=cm&tf=0" );
  
  if ( to != null && ! to.isEmpty() )
    result.append( "&to=" ).append( to );
  if ( cc != null && ! cc.isEmpty() )
    result.append( "&cc=" ).append( cc );
  if ( subject != null && ! subject.isEmpty() ) {
    try {
      result.append( "&su=" ).append( encode( subject ) );
    }
    catch ( UnsupportedEncodingException e ) {
      e.printStackTrace();
    }
  }
  if ( body != null && ! body.isEmpty() ) {
    try {
      result.append( "&body=" ).append( encode( body ) );
    }
    catch ( UnsupportedEncodingException e ) {
      e.printStackTrace();
    }
  }
  return result.toString();
}
private static String encode( String param ) throws UnsupportedEncodingException
{    
  byte[] utf8 = param.getBytes( "utf-8" );
  StringBuilder result = new StringBuilder( param.length() * 3 );
  for ( int i = 0; i < utf8.length; i++ )
  {
    String c = Integer.toString( utf8[ i ] & 0xFF, 16 );
    result.append( '%' ).append( c.length() == 1 ? "0" + c : c );
  }
  return result.toString();
}

Ähnliche Beiträge

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert