W3C home > Mailing lists > Public > www-validator-cvs@w3.org > October 2009

2006/unicorn/src/org/w3c/unicorn/util Mail.java,1.1,1.2

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Fri, 09 Oct 2009 06:43:58 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1Mw9Co-0006ur-O9@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/util
In directory hutz:/tmp/cvs-serv26569/src/org/w3c/unicorn/util

Modified Files:
	Mail.java 
Log Message:
working on utf-8 mail output

Index: Mail.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/util/Mail.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Mail.java	7 Oct 2009 15:54:40 -0000	1.1
+++ Mail.java	9 Oct 2009 06:43:56 -0000	1.2
@@ -1,6 +1,13 @@
 package org.w3c.unicorn.util;
 
+import java.io.ByteArrayOutputStream;
 import java.io.CharArrayWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.List;
@@ -18,7 +25,9 @@
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
 
+import org.apache.commons.lang.StringEscapeUtils;
 import org.w3c.unicorn.exceptions.UnicornException;
+import org.w3c.unicorn.output.AttachmentOutputFormater;
 import org.w3c.unicorn.output.OutputFormater;
 
 public class Mail {
@@ -51,24 +60,62 @@
 			
 			if (outputFormaters.size() > 1) {
 				// New multipart message
-				Multipart mp = new MimeMultipart("alternative");
-				for (OutputFormater outputFormater : outputFormaters) {
+				MimeMultipart mp = new MimeMultipart();
+				for (OutputFormater outputFormater : outputFormaters) {		
 					MimeBodyPart bodyPart = new MimeBodyPart();
-					bodyPart.addHeader("Content-Type", outputFormater.getMimeType() + ", charset=UTF-8");
-					CharArrayWriter writer = new CharArrayWriter();
-					outputFormater.produceOutput(contextObjects, writer);
-					writer.close();
-					bodyPart.setContent(writer.toString(), outputFormater.getMimeType());
+					if (outputFormater instanceof AttachmentOutputFormater)
+						bodyPart.setFileName(((AttachmentOutputFormater) outputFormater).getFileName());
+
+					ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+					OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
+					
+					outputFormater.produceOutput(contextObjects, outputStreamWriter);
+					
+					
+					
+					try {
+						outputStreamWriter.close();
+						byteArrayOutputStream.close();
+					} catch (IOException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
+					
+					
+					
+					bodyPart.setContent(byteArrayOutputStream.toString("UTF-8"), outputFormater.getMimeType());
+					
+					bodyPart.setHeader("Content-Type", outputFormater.getMimeType() + "; charset=UTF-8");
+					bodyPart.setHeader("Content-Transfer-Encoding", "8bit");
 					mp.addBodyPart(bodyPart);
 				}
 				msg.setContent(mp);
 			} else {
-				CharArrayWriter writer = new CharArrayWriter();
-				outputFormaters.get(0).produceOutput(contextObjects, writer);
-				writer.close();
-				msg.setContent(writer.toString(), outputFormaters.get(0).getMimeType());
+				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+				OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
+				//CharArrayWriter wr = new CharArrayWriter();
+				
+				outputFormaters.get(0).produceOutput(contextObjects, outputStreamWriter);
+				
+				//outputStreamWriter.write("んのこと仲介手数料もなし");
+				
+				try {
+					outputStreamWriter.close();
+					byteArrayOutputStream.close();
+				} catch (IOException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+				System.out.println(byteArrayOutputStream.toString("UTF-8"));
+				
+				
+				msg.setContent(byteArrayOutputStream.toString("UTF-8"), outputFormaters.get(0).getMimeType() + "; charset=UTF-8");
+				msg.writeTo(new FileOutputStream(new File(Property.get("UPLOADED_FILES_REPOSITORY") + "/test1.txt")));
+				msg.setHeader("Content-Type", outputFormaters.get(0).getMimeType() + "; charset=UTF-8");
+				msg.setHeader("Content-Transfer-Encoding", "7bit");
+				
 			}
-			
+			msg.writeTo(new FileOutputStream(new File(Property.get("UPLOADED_FILES_REPOSITORY") + "/test.txt")));
 			Transport.send(msg);
 		} catch (UnsupportedEncodingException e) {
 			// TODO Auto-generated catch block
@@ -76,6 +123,9 @@
 		} catch (MessagingException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
 		}
 	}
 	
Received on Friday, 9 October 2009 06:44:02 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:17:39 UTC