title: EMail subject get set in CP1252 encoding even though the string is in UTF-8
body: <p>The file new1.txt contains UTF-8 string which is sent as subject. But the received email will show the string in CP1252 format. But if I set console encoding by going to Run Config->Common Tab and set console encoding as UTF-8, I can see UTF-string properly in the recieved email. I using google server for this test:</p>
  
  <p>Not working:
  æ—¥æœ¬ : Partner Name: ã‚¢ã‚¹ãƒˆãƒ©ã‚¼ãƒ?ã‚«æ ªå¼?ä¼šç¤¾ : </p>
  
  <p>Working:
   日本 : Partner Name: アストラゼネカ株式会社 </p>
  
  <p>My Code:
          final String username = "xxxxx@gmail.com";
          final String password = "xxxxxx";</p>
  
  <pre><code>    Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.socketFactory.class",
              "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "465");
  
      Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username,password);
              }
          });
  
      try {
          File fileDir = new File("c:\\new1.txt");
  
          BufferedReader in = new BufferedReader(
             new InputStreamReader(
                        new FileInputStream(fileDir), "UTF-8"));
  
          String str;
          String str1 ="";
  
          while ((str = in.readLine()) != null) {
              str1 += str;
          }
  
          Message message = new MimeMessage(session);
          MimeMultipart mp = new MimeMultipart();
          message.setFrom(new InternetAddress("xxxxx@gmail.com"));
          message.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse("xxxx@xxxxx.com"));
          message.setSubject(str1);
          MimeBodyPart body = new MimeBodyPart();
          body.setContent("This is a Test EMail. Please ignore", "text/html");
          body.setDisposition(MimeBodyPart.INLINE);
          mp.addBodyPart(body);
          byte[] attachmentData = str1.getBytes();
          DataHandler dh = new DataHandler(new ByteArrayDataSource(attachmentData,"application/octet-stream"));
          MimeBodyPart attachment = new MimeBodyPart();
          attachment.setDataHandler(dh);
          attachment.setDisposition(MimeBodyPart.ATTACHMENT);
          attachment.setFileName("new1.txt");
          mp.addBodyPart(attachment);
  
          message.setContent(mp);
          Transport.send(message);
  
          System.out.println("Done");
  </code></pre>
  
  <p>thanks
  Madhu</p>
  
  <p>Following are the code changes I made in my J2EE app. In this it is adding 3 files as attachments. One of the 3 files is the subject itself. In the attachment (Subject.txt) conent is fine.</p>
  
  <pre><code>    MimeMessage message = new MimeMessage(s);
  
      ....
  
      message.setSubject(subject,"UTF-8");// MimeUtility.encodeText(subject,"UTF-8", "B"));
  
      ...
  
              MimeBodyPart body = new MimeBodyPart();
              body.setContent(sm.getMailBody(), "text/html; charset=UTF-8");
              body.setDisposition(MimeBodyPart.INLINE);
              mp.addBodyPart(body);
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/YpDEW.png" rel="nofollow">The debug message. I could not add it as text</a></p>