1

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

在此处输入图像描述 我用来发送文件的代码如下:

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

非常有趣的是,收到的是这样的:

在此处输入图像描述 当我从我的网络浏览器执行相同操作时,可以正确接收邮件。以下是附件的详细信息(我们通过单击 gmail 收件箱中的显示原始选项来获取它):

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”,但没有区别。谁能帮我这个?

4

2 回答 2

1

试试这个代码。它适用于我的语言,如मराठी、हिन्दी、英语等。

message.setContent(multipart, "text/html; charset=utf-8");
于 2019-01-02T10:17:22.297 回答
0

阅读这个这个并简化你的程序。

您不能为多部分设置编码。改变

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

message.setContent(multipart);

由于您的 multipart 仅包含一个部分,因此您根本不需要 multipart,但我们现在将忽略它。

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

于 2013-09-03T18:06:02.913 回答