rahulserver
rahulserver

Reputation: 11205

Hindi html file converted to chinese while sending via javamail

I am trying to send an html file containing some hindi content using javamail. Here is a screenshot of the file content:

enter image description here The code I am using to send the file is as:

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;

import java.util.*;

/**
 * Simple Class to send an email using JavaMail API (javax.mail) and Gmail SMTP server
 * @author Dunith Dhanushka, [email protected]
 * @version 1.0
 */
public class GmailSender {

    private static String HOST = "smtp.gmail.com";
    private static String USER = "[email protected]";
    private static String PASSWORD = "mypassword";
    private static String PORT = "465";
    private static String FROM = "[email protected]";
    private static String TO = "[email protected]";

    private static String STARTTLS = "true";
    private static String AUTH = "true";
    private static String DEBUG = "true";
    private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static String SUBJECT = "Testing JavaMail API";
    private static String TEXT = "Message with attachment from my java application. Just ignore it";

    public static synchronized void send() {
        //Use Properties object to set environment properties
        Properties props = new Properties();

        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.user", USER);

        props.put("mail.smtp.auth", AUTH);
        props.put("mail.smtp.starttls.enable", STARTTLS);
        props.put("mail.smtp.debug", DEBUG);

        props.put("mail.smtp.socketFactory.port", PORT);
        props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");

        try {

            //Obtain the default mail session
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);

            //Construct the mail message
            MimeMessage message = new MimeMessage(session);
            message.setText(TEXT);
            message.setSubject(SUBJECT);
            message.setFrom(new InternetAddress(FROM));
            message.addRecipient(RecipientType.TO, new InternetAddress(TO));

            //add attachments
            MimeBodyPart messageBodyPart = new MimeBodyPart();

            Multipart multipart = new MimeMultipart();

            messageBodyPart = new MimeBodyPart();
            String file = "filenamewithpath";
            String fileName = "attachmentName.html";
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);

            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart,"UTF-8");
            message.saveChanges();



            //Use Transport to deliver the message
            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, USER, PASSWORD);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        }

    }

    public static void main(String[] args) {
        GmailSender.send();
        System.out.println("Mail sent successfully!");
    }

And very interestingly what is received is this:

enter image description here When I do the same from my web browser, the mail is received correctly. Here is the details of the attached part(we get it by clicking the show original option in gmail inbox):

Content-Type: text/html; charset=UTF-16BE; name="filename.html"
Content-Disposition: attachment; filename="filaname.html"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_hl0znk4d0

So the encoding is "UTF-16BE". I have tried to change the encoding to "UTF-16BE" from "UTF-8" but no difference. Can anyone help me with this?

Upvotes: 1

Views: 902

Answers (2)

Deva
Deva

Reputation: 2121

try this code. Its works for my language like मराठी, हिन्दी , English etc.

message.setContent(multipart, "text/html; charset=utf-8");

Upvotes: 1

Bill Shannon
Bill Shannon

Reputation: 29971

Read this and this and simplify your program.

You can't set an encoding for a multipart. Change

message.setContent(multipart,"UTF-8");

to

message.setContent(multipart);

Since your multipart only contains a single part, you don't really need the multipart at all, but we'll ignore that for now.

The file that you're attaching will be assumed to be using the default charset for the locale the server is running in. That may not be "utf-8". In fact, it may be "utf-16be". If the file really is using utf-8, try setting the System property "mail.mime.charset" to "utf-8".

Upvotes: 0

Related Questions