Ciao a tutti,

ho un problema con l'encoding nell'invio di mail.

il testo che devo inviare e' stato scritto il unicode di seguito un esempio:

codice:
Cher/Ch\u00E8re {0} {1},\n\nMerci d'avoir achet\u00E9
come potete intuire dalla struttura del testo utilizzo la classe MessageFormat per formattare il testo.
(ho provato a guardarmi anache questa classe ma non ho trovato traccia di encoding, charset o altro)

Come Charset prima di inviare la mail setto ISO-8859-1,
ma il mail server sembra non prendere in cosiderazione il Charset da me imposato...
o meglio lo imposta nell'header della mail ma invia la mail in UTF-8...

vi posto anche il frammento dell'header della mail:

codice:
Subject: =?ISO-8859-1?Q?Confirmation_de_r=E9servation_vol_a=E9rien?=MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
il risultato e' scontato...
Il client prova ad interpretare il testo usando la codifica ISO...
ma essendo stato scritto il UTF fa' casino.
codice:
Cher/Chère <nome> <cognome>,

Merci d'avoir acheté
ho fatto delle prove, se faccio leggere a ThunderBird la mail codificandola in UTF-8 la interpreta correttamente...

non so piu' come poter affrontare il porblema ho provato in mille modi diversi, ma il risultato e' sempre lo stesso...

Per completezza vi posto anche un paio di soluzioni che ho provato ad adottare senza riuscire a trovare una soluzione...
magari sbaglio da qualche parte e non me ne sono accorto...

PRIMA SOLUZIONE:

codice:
public static void main(String[] args) throws UnsupportedEncodingException
  {
    byte[] test2;
    String test = "Arriv\u00E9:", test3;
    
    test2 = test.getBytes("ISO-8859-1");
    test3 = new String(test2, "ISO-8859-1");
    
    System.out.println(test);
    System.out.println(test2);
    System.out.println(test3);
    
  }
SECONDA SOLUZIONE:

codice:
public static void main (String[] args) throws UnsupportedEncodingException, CharacterCodingException
  {
    Charset isoCharset = Charset.forName("ISO-8859-1");
    Charset utfCharset = Charset.forName("UTF-8");
    
    CharsetDecoder decoder = utfCharset.newDecoder();
    CharsetEncoder encoder = isoCharset.newEncoder();
    
    String text = new String("Cher/Ch\u00E8re {0} {1},\n\nMerci d'avoir achet\u00E9".getBytes("UTF-8"));
    
    System.out.println("text:" + text);
      
    ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(text));
    CharBuffer charBuffer = decoder.decode(byteBuffer);
    String text1 = charBuffer.toString();
    System.out.println("text1: " + text1);
  }
Le ho postate in dei main almeno cosi' se volete potete provarli...

Scusate per il papiro infinito... spero di essere stati chiaro nel caso chiedete pure...

grazie a tutti