0

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!!");

For reference, Subject with chinese name looks like this

halfer
  • 19,824
  • 17
  • 99
  • 186
New2Java
  • 273
  • 1
  • 4
  • 19
  • Have you checked https://stackoverflow.com/questions/15044027/utf-8-charset-doesnt-work-with-javax-mail? – Ori Marko Jul 26 '17 at 12:21
  • Before checking this post, I have tried these mentioned solutions. Specifically setSubject(subject,charset) did not work in eclipse ide. It only took one argument. May be after reading the post I am thinking that my input is not encoded while collecting itself. – New2Java Jul 26 '17 at 12:30
  • Can anyone please enlighten me on how to use setText() with charset argument to set subject? – New2Java Jul 26 '17 at 12:32
  • The character set for mail headers is *always* US-ASCII (if you make no assumptions about the SMTP capabilities). You can use either base-64 encoding or quoted-printable, using [encoded words as in RFC 1522](https://tools.ietf.org/html/rfc1522). – RealSkeptic Jul 26 '17 at 12:42

0 Answers0