#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <glib.h>
#include <gmime/gmime.h>

char *sbj[] = { /* Japanese mail subjects. */
	     "Re: はじめまして", /* only hiragana. */
	     "Re: 自己紹介",     /* only chinese characters. */
	     "Re: ご挨拶",       /* hiragana and chinese characters. */
	     NULL,
};

int main(int argc, char **argv) {
  GMimeStream *stream;
  GMimeMessage *msg;
  int i;
  iconv_t cd;
  char _in[256], _out[256], *in, *out, *str;
  size_t inlen,outlen;

  g_mime_init (0);
  msg = g_mime_message_new(0);
  stream = g_mime_stream_fs_new (dup (1));
  for (i=0; sbj[i] != (char *)0; i++) {
    in = &_in[0];
    out = &_out[0];
    strcpy(in, sbj[i]);
    inlen = strlen(in);
    memset(out, 0, 256);
    outlen = sizeof(_out);
    str = &out[0];
    cd = iconv_open("utf-8", "euc-jp");
    if (iconv(cd, &in, &inlen, &out, &outlen) == -1) {
      perror("iconv error");
      exit(1);
    }
    iconv_close(cd);

    g_mime_message_set_subject(msg,str);
    g_mime_header_write_to_stream (((GMimeObject *) msg)->headers, stream);
    g_mime_stream_flush (stream);
  }
  g_object_unref (stream);
  g_object_unref (msg);
  return 0;
}
