diff -cr evolution-1.4.6/camel/camel-charset-map.c evolution-1.4.6+localheader/camel/camel-charset-map.c
*** evolution-1.4.6/camel/camel-charset-map.c	2003-02-26 04:43:22.000000000 +0900
--- evolution-1.4.6+localheader/camel/camel-charset-map.c	2004-04-30 22:29:04.000000000 +0900
***************
*** 207,212 ****
--- 207,213 ----
  
  #include <glib.h>
  #include <glib/gunicode.h>
+ #include <gal/util/e-iconv.h>
  #include <locale.h>
  #include <ctype.h>
  #ifdef ENABLE_THREADS
***************
*** 298,303 ****
--- 299,351 ----
  	return camel_charset_best_name (&charset);
  }
  
+ /* copy from composer/e-msg-composer.c#best_encoding() */
+ static int
+ camel_charset_can_convert(char *in, size_t inlen, const char *charset)
+ {
+ 	char *out, outbuf[256];
+ 	size_t outlen;
+ 	int status;
+ 	iconv_t cd;
+ 	
+ 	if (!charset)
+ 		return 0;
+ 	
+ 	cd = e_iconv_open (charset, "utf-8");
+ 	if (cd == (iconv_t) -1)
+ 		return 0;
+ 	
+ 	do {
+ 		out = outbuf;
+ 		outlen = sizeof (outbuf);
+ 		status = e_iconv (cd, &in, &inlen, &out, &outlen);
+ 	} while (status == (size_t) -1 && errno == E2BIG);
+ 	e_iconv_close (cd);
+ 	
+ 	if (status == (size_t) -1)
+ 		return 0;
+ 	
+ 	return 1;
+ }
+ 
+ /* copy from composer/e-msg-composer.c#best_charset() */
+ const char *
+ camel_charset_select(const char *in, int len, GSList *prefer_charsets)
+ {
+ 	const char *charset;
+ 	GSList *list;
+ 
+ 	for (list = prefer_charsets; list; list = g_slist_next(list)) {
+ 		charset = (char *)list->data;
+ 		if (camel_charset_can_convert (in, len, charset) != 0)
+ 			return charset;
+ 	}
+ 
+ 	/* Try to find something that will work */
+ 	charset = camel_charset_best (in, len);
+ 	return charset;
+ }
+ 
  
  /**
   * camel_charset_iso_to_windows:
diff -cr evolution-1.4.6/camel/camel-charset-map.h evolution-1.4.6+localheader/camel/camel-charset-map.h
*** evolution-1.4.6/camel/camel-charset-map.h	2003-02-26 04:43:22.000000000 +0900
--- evolution-1.4.6+localheader/camel/camel-charset-map.h	2004-04-30 22:28:20.000000000 +0900
***************
*** 27,32 ****
--- 27,33 ----
  #endif /* __cplusplus */
  
  #include <iconv.h>
+ #include <glib.h>
  
  typedef struct _CamelCharset CamelCharset;
  
***************
*** 42,47 ****
--- 43,49 ----
  
  /* helper function */
  const char *camel_charset_best(const char *in, int len);
+ const char *camel_charset_select(const char *in, int len, GSList *prefer_charsets);
  
  const char *camel_charset_iso_to_windows (const char *isocharset);
  
diff -cr evolution-1.4.6/camel/camel-mime-message.c evolution-1.4.6+localheader/camel/camel-mime-message.c
*** evolution-1.4.6/camel/camel-mime-message.c	2003-06-12 00:59:30.000000000 +0900
--- evolution-1.4.6+localheader/camel/camel-mime-message.c	2004-04-30 22:28:20.000000000 +0900
***************
*** 137,142 ****
--- 137,143 ----
  	mime_message->date_received = CAMEL_MESSAGE_DATE_CURRENT;
  	mime_message->date_received_offset = 0;
  	mime_message->message_id = NULL;
+ 	mime_message->prefer_charsets = NULL;
  }
  
  static void           
***************
*** 156,161 ****
--- 157,165 ----
  	
  	g_hash_table_foreach (message->recipients, unref_recipient, NULL);
  	g_hash_table_destroy (message->recipients);
+ 
+ 	g_slist_foreach(message->prefer_charsets, g_free, NULL);
+ 	g_slist_free(message->prefer_charsets);
  }
  
  
***************
*** 322,328 ****
  	
  	g_free (mime_message->subject);
  	mime_message->subject = g_strstrip (g_strdup (subject));
! 	text = header_encode_string((unsigned char *)mime_message->subject);
  	CAMEL_MEDIUM_CLASS(parent_class)->set_header(CAMEL_MEDIUM (mime_message), "Subject", text);
  	g_free (text);
  }
--- 326,332 ----
  	
  	g_free (mime_message->subject);
  	mime_message->subject = g_strstrip (g_strdup (subject));
! 	text = header_encode_string_prefer((unsigned char *)mime_message->subject, mime_message->prefer_charsets);
  	CAMEL_MEDIUM_CLASS(parent_class)->set_header(CAMEL_MEDIUM (mime_message), "Subject", text);
  	g_free (text);
  }
***************
*** 421,426 ****
--- 425,439 ----
  	}
  }
  
+ void
+ camel_mime_message_append_prefer_charset(CamelMimeMessage *mime_message, const char *charset)
+ {
+ 	g_assert (mime_message);
+ 
+ 	mime_message->prefer_charsets =
+ 		g_slist_append(mime_message->prefer_charsets, g_strdup(charset));
+ }
+ 
  const char *
  camel_mime_message_get_source (CamelMimeMessage *mime_message)
  {
diff -cr evolution-1.4.6/camel/camel-mime-message.h evolution-1.4.6+localheader/camel/camel-mime-message.h
*** evolution-1.4.6/camel/camel-mime-message.h	2003-03-21 01:43:26.000000000 +0900
--- evolution-1.4.6+localheader/camel/camel-mime-message.h	2004-04-30 22:28:20.000000000 +0900
***************
*** 73,78 ****
--- 73,79 ----
  	CamelInternetAddress *from;
  
  	GHashTable *recipients;	/* hash table of CamelInternetAddress's */
+ 	GSList *prefer_charsets;
  };
  
  typedef struct {
***************
*** 120,125 ****
--- 121,128 ----
  void                        camel_mime_message_set_source         (CamelMimeMessage           *mime_message,
  								   const char                 *identity);
  const char                 *camel_mime_message_get_source         (CamelMimeMessage           *mime_message);
+ void                        camel_mime_message_append_prefer_charset(CamelMimeMessage         *mime_message,
+ 								     const char               *charset);
  								   
  
  /* utility functions */
diff -cr evolution-1.4.6/camel/camel-mime-utils.c evolution-1.4.6+localheader/camel/camel-mime-utils.c
*** evolution-1.4.6/camel/camel-mime-utils.c	2003-10-28 22:10:02.000000000 +0900
--- evolution-1.4.6+localheader/camel/camel-mime-utils.c	2004-04-30 22:28:20.000000000 +0900
***************
*** 1319,1324 ****
--- 1319,1329 ----
  char *
  header_encode_string (const unsigned char *in)
  {
+ 	return header_encode_string_prefer(in, NULL);
+ }
+ char *
+ header_encode_string_prefer (const unsigned char *in, GSList *prefer_charsets)
+ {
  	const unsigned char *inptr = in, *start, *word;
  	gboolean last_was_encoded = FALSE;
  	gboolean last_was_space = FALSE;
***************
*** 1385,1391 ****
  					g_string_append_c (out, ' ');
  				
  				rfc2047_encode_word (out, start, inptr - start,
! 						     camel_charset_best (start, inptr - start), IS_ESAFE);
  				last_was_encoded = TRUE;
  				break;
  			}
--- 1390,1396 ----
  					g_string_append_c (out, ' ');
  				
  				rfc2047_encode_word (out, start, inptr - start,
! 						     camel_charset_select (start, inptr - start, prefer_charsets), IS_ESAFE);
  				last_was_encoded = TRUE;
  				break;
  			}
***************
*** 1431,1437 ****
  				g_string_append_c (out, ' ');
  			
  			rfc2047_encode_word (out, start, inptr - start,
! 					     camel_charset_best (start, inptr - start - 1), IS_ESAFE);
  			break;
  		}
  	}
--- 1436,1442 ----
  				g_string_append_c (out, ' ');
  			
  			rfc2047_encode_word (out, start, inptr - start,
! 					     camel_charset_select (start, inptr - start, prefer_charsets), IS_ESAFE);
  			break;
  		}
  	}
diff -cr evolution-1.4.6/camel/camel-mime-utils.h evolution-1.4.6+localheader/camel/camel-mime-utils.h
*** evolution-1.4.6/camel/camel-mime-utils.h	2003-03-21 01:43:26.000000000 +0900
--- evolution-1.4.6+localheader/camel/camel-mime-utils.h	2004-04-30 22:28:20.000000000 +0900
***************
*** 174,179 ****
--- 174,180 ----
  /* decode/encode a string type, like a subject line */
  char *header_decode_string (const char *in, const char *default_charset);
  char *header_encode_string (const unsigned char *in);
+ char *header_encode_string_prefer (const unsigned char *in, GSList *prefer_charsets);
  
  /* encode a phrase, like the real name of an address */
  char *header_encode_phrase (const unsigned char *in);
diff -cr evolution-1.4.6/composer/e-msg-composer.c evolution-1.4.6+localheader/composer/e-msg-composer.c
*** evolution-1.4.6/composer/e-msg-composer.c	2003-10-07 02:07:16.000000000 +0900
--- evolution-1.4.6+localheader/composer/e-msg-composer.c	2004-04-30 22:28:20.000000000 +0900
***************
*** 364,369 ****
--- 364,370 ----
  	CamelMimePart *part;
  	CamelException ex;
  	int i;
+ 	GSList *prefer_charsets = NULL;
  	
  	if (composer->persist_stream_interface == CORBA_OBJECT_NIL)
  		return NULL;
***************
*** 376,381 ****
--- 377,384 ----
  	}
  	
  	new = camel_mime_message_new ();
+ 	camel_mime_message_append_prefer_charset (new, composer->charset);
+ 	camel_mime_message_append_prefer_charset (new, composer_get_default_charset_setting ());
  	e_msg_composer_hdrs_to_message (hdrs, new);
  	for (i = 0; i < composer->extra_hdr_names->len; i++) {
  		camel_medium_add_header (CAMEL_MEDIUM (new),
