I am using JAVA-MAIL API to send user mails where the subject line contains user's name. For any English alphabet name, the name is properly displayed in the subject line (which I am setting with message.setSubject()) but when I entered a Chinese name, it didn't reflect properly in the subject line.
The same Chinese name is exactly displayed in the body part where I am trying to show these captured entities with setContent(). I tried to change the encoding format but then I got UnsupportedEncodingException error. Please help me how can I get the name exactly as it is (may it be english/chinese/japanese) in subject line.
Here is the code I am using:
public class RegistrationController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("fullname");
String Addr = request.getParameter("address");
String age = request.getParameter("age");
String Qual = request.getParameter("qual");
String Persent = request.getParameter("percent");
String Year = request.getParameter("yop");
if (name.isEmpty() || Addr.isEmpty() || age.isEmpty() || Qual.isEmpty()
|| Persent.isEmpty() || Year.isEmpty()) {
RequestDispatcher rd = request
.getRequestDispatcher("registration.jsp");
out.println("<font color=red>OOPS!! YOU MISSED OUT SOME FIELDS. FILL THEM AGAIN</font>");
rd.include(request, response);
} else {
try {
final String username = "abc@gmail.com";
final String password = "*****";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xyz@gmail.com"));
message.setSubject("THANK YOU " +name+ " FOR SUBSCRIBING WITH US!!");
message.setContent(
"<h3>Please review your entered details below.</h3><table><tr><td>Name</td><td>"
+ name
+ "</td></tr><tr><td>Address</td><td>"
+ Addr
+ "</td></tr><tr><td>Age</td><td>"
+ age
+ "</td></tr><tr><td>Qualification</td><td>"
+ Qual
+ "</td></tr><tr><td>Percentage</td><td>"
+ Persent
+ "</td></tr><tr><td>Passout</td><td>"
+ Year
+ "</td></tr></table>", "text/html");
Transport.send(message);
System.out.println("mail sent successful");
} catch (MessagingException ex) {
ex.printStackTrace();
} finally {
RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
rd.forward(request, response);
}
}
}
When I tried the below code, It threw UnsupportedEncodingException.
message.setSubject("THANK YOU " +new String(name.getBytes("UTF-8"))+ " FOR SUBSCRIBING WITH US!!");