通过javamail发送时,Hindi html文件转换为中文

时间:2013-08-31 18:16:39

标签: java encoding javamail

我正在尝试使用javamail发送包含一些印地文内容的html文件。以下是文件内容的屏幕截图:

enter image description here 我用来发送文件的代码如下:

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, dunithd@gmail.com
 * @version 1.0
 */
public class GmailSender {

    private static String HOST = "smtp.gmail.com";
    private static String USER = "myemail@gmail.com";
    private static String PASSWORD = "mypassword";
    private static String PORT = "465";
    private static String FROM = "recipientemail@gmail.com";
    private static String TO = "toemail@gmail.com";

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

非常有趣的是收到的是:

enter image description here 当我从我的网络浏览器中执行相同操作时,正确接收邮件。以下是附件的详细信息(我们通过点击gmail收件箱中的show original选项获取它):

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

所以编码是“UTF-16BE”。我试图将编码从“UTF-8”更改为“UTF-16BE”,但没有区别。任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

阅读thisthis并简化您的计划。

您无法为多部分设置编码。变化

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

message.setContent(multipart);

由于你的多部分只包含一个部分,你根本不需要多部分,但我们暂时忽略它。

您将附加的文件将被假定为使用服务器运行的语言环境的默认字符集。这可能不是“utf-8”。事实上,它可能是“utf-16be”。如果文件确实使用utf-8,请尝试将系统属性“mail.mime.charset”设置为“utf-8”。

答案 1 :(得分:0)

尝试此代码。它适用于我的语言,例如मराठी,हिन्दी和English等。

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