darangho 0 Newbie Poster

Hi everyone,

I am trying to write a code that receives an input from textarea,
transfer that input to servlet and
send an email with that input using javamail API.

It seems that the codes work fine with english characters,
but it doesn't work with non-english characters (i am using korean for this),
It sends an email with some weird characters inside it.

I am using apache tomcat 6.0 with it,
and its server.xml looks like this

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" URIEncoding="euc-kr" redirectPort="8443"/>

Is there anyone can help me with this issue?
I am desperate for help!

Thanks.


Here are the lists of my code.

contact.jsp

<%@ page language="java" contentType="text/html; charset=euc-kr"
    pageEncoding="euc-kr"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/contact.js"></script>
<link href="style/common.css" rel="stylesheet" type="text/css" />
<link href="style/contact.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<title>Contact us</title>
</head>
<body>
	<jsp:include page="tabs.jsp">
			<jsp:param name="pressedon" value="2" />
	</jsp:include>
	
	<!-- Contents -->
	<table bgcolor="#F5F5F5" align="center" width="960px" border="0"
		cellspacing="0" cellpadding="0">
	
		<!-- Top border -->
		<tr>
			<td width="20" height="20" background="img/round_lt.gif"><img
				src="img/spacer.gif" width="20" height="20" /></td>
			<td width="920" height="20" colspan="2" background="img/bg_t.gif"><img
				src="img/spacer.gif" width="920" height="20" /></td>
			<td width="20" height="20" background="img/round_rt.gif"><img
				src="img/spacer.gif" width="20" height="20" /></td>
		</tr>
	
		<tr>
			<!-- Left border -->
			<td width="20" background="img/bg_l.gif">&nbsp;</td>
	
			<!-- Core Content -->
			<td width="920" colspan="2" valign="top"
				style="word-break: break-all;">
				<!-- Input for Path -->
				<table align="center" cellpadding="0" cellspacing="0" style="width: 610px;">
					<colgroup>
						<col width="200px">
						<col width="610px">
					</colgroup>
					<tr>
						<td class="label_input noborder">
							<div align="center">
								<img class="blackbox" width=200px src="img/support.jpg"></img>
								<br/><br/>
							</div>
						</td>
						<td class="label_input noborder">
							You may use this email webform to contact us regarding any questions that you may have with. <br/><br/>

							Please be as thorough as possible when filling out the form, and 
							include any additional information that you think will assist us in evaluating your inquiry. 
							<br/><br/><br/>
						</td>
					</tr>
					<tr> 		
						<td colspan=2 class="label_input noborder">	
							<div align="center" id="status"></div>
						</td>	
					</tr>
					<tr> 
						<!-- Name -->
						<th class="label_input">
							Name:</th>			
						<td class="listbox">	
							<input type="text" class="text" id="name" size="25">
						</td>	
					</tr>
					<tr> 
						<!-- Email -->
						<th class="label_input">
							Email:</th>			
						<td class="listbox">	
							<input type="text" class="text" id="email" size="25">
						</td>	
					</tr>
					<tr>
						<!-- Message -->
						<th colspan="2" class="label_input">
							Message:</th>
					</tr>
					<tr>
						<td id="msg_td" colspan="2">
							<div align="center">
								<textarea class="textarea_obj" id="message"></textarea>
							</div>
						</td>
					</tr>
					<tr>	 					
						<td colspan=2 class="listbox">
							<div align="center">
								<button type="button" id="submit" onclick="checkFilled()" >Submit</button>
							</div>
				        </td>
					</tr>
				</table>
			</td>
			<!-- Right border -->
			<td width="20" background="img/bg_r.gif">&nbsp;</td>
		</tr>
		<!-- Bottom border -->
		<tr>
			<td width="20" height="20" background="img/round_lb.gif"><img
				src="img/spacer.gif" width="20" height="20" alt="" /></td>
			<td width="920" height="20" colspan="2" background="img/bg_b.gif"><img
				src="img/spacer.gif" width="920" height="20" alt="" /></td>
			<td width="20" height="20" background="img/round_rb.gif"><img
				src="img/spacer.gif" width="20" height="20" alt="" /></td>
		</tr>
	</table>
</body>
</html>

contact.js

//Submit the path information to database
function checkFilled() {
	var nameObj = document.getElementById("name");
	var name = trim(nameObj.value).toUpperCase();
	
	var emailObj = document.getElementById("email");
	var email = trim(emailObj.value);
	
	var msgObj = document.getElementById("message");
	var msg = trim(msgObj.value);
		
	var statObj = document.getElementById("status");
	statObj.className = "noborder";
	statObj.innerHTML = "";
	
	//One or more of the fields are missing
	if(email.length == 0 || name.length == 0 || msg.length == 0) {
		statObj.innerHTML = "One or more fields are empty. Please fill in the fields highlighted in red";
		statObj.className = "noborder red_text";
		
		if(email.length == 0) {
			emailObj.className = "listbox error"; 
		}
		else {
			emailObj.className = "listbox"; 
		}
		
		if(name.length == 0) {
			nameObj.className = "listbox error"; 
		}
		else {
			nameObj.className = "listbox"; 
		}
		
		if(msg.length == 0) {
			msgObj.className = "textarea textarea_obj error"; 
		}
		else {
			msgObj.className = "textarea textarea_obj noborder";
		}
	}
	//All of the fields are filled
	else {
		statObj.innerHTML = "";
		statObj.className = "noborder";
		emailObj.className = "listbox"; 
		nameObj.className = "listbox"; 
		msgObj.className = "textarea textarea_obj noborder";
		
		var content = "Sender's name: " + name + "\n\n" + 
		"Reply to: " + email + "\n\n" + 
		"Message from sender: \n" + msg + "\n"; 
		
		var param = "name=" + name + "&msg="+content;		
			
		var url = "submitMessage";
		submit(param, url);
	}
};


function submit(param, url){
	alert(param);
	
	//Lock all field components while submitting
	//lockAll();
	var statObj = document.getElementById("status");
	statObj.innerHTML = "Submitting...";
	statObj.className = "noborder red_text";
	
	var ajax = new ajaxCall(param, url, function (status, text, xml) {
		if (status == 200) {
			var back = xml.getElementsByTagName("status")[0].firstChild.nodeValue;
			
			if(back == "Submission Success") {
				statObj.className = "noborder green_text";
				statObj.innerHTML = back;
				//clearAll();
			}
			else {
				statObj.className = "noborder red_text";
				statObj.innerHTML = back;
			}
		}
		
		//Unlock all field components back to original
		//unlockAll();
	});	
};

function trim(str) {
	var newStr = str.replace(/^\s*/, "").replace(/\s*$/, "");
	newStr = newStr.replace(/\s{2,}/," ");
	return newStr;
};


//Ajax general function for asynchronous call
function ajaxCall(param, url, callback) {
	var xmlhttp = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			callback(xmlhttp.status, xmlhttp.responseText, xmlhttp.responseXML);
		}
	};
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(param);
};

sendMessage.java

package hakcs;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class submitMessage
 */
public class submitMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "@gmail.com";
    private static final String SMTP_AUTH_PWD  = "";   
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("euc-kr");
		
		PrintWriter out = response.getWriter();
		
		String name = request.getParameter("name");

		System.out.println("Name: " + name);
	
		String msg = request.getParameter("msg");
						
		String status = "";
		
		Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
		
		
        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(false);
        try {
			Transport transport = mailSession.getTransport();
			MimeMessage message = new MimeMessage(mailSession);
			
	        try {
	        	message.setHeader("Content-Type", "text/plain; charset=euc-kr");
				message.setSubject(name, "euc-kr");
				message.setContent(msg, "text/plain; charset=euc-kr");
			    message.setFrom(new InternetAddress(SMTP_AUTH_USER));

			    message.addRecipient(Message.RecipientType.TO, new InternetAddress(SMTP_AUTH_USER));
			    
			    transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
		    
			    
		        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
		        transport.close();
		        
		        status = "Submission Success";
			} 
	        catch (MessagingException e) {
				// TODO Auto-generated catch block
	        	status = "Submission Failed";
	        	e.printStackTrace();
				
			}
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			status = "Submission Failed";
			e.printStackTrace();
		}
        		
		// Send resulting xml to user
	    response.setContentType("text/xml");
	    response.setHeader("Cache-Control", "no-cache");
	    out.print("<status>" + status + "</status>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}