我正在尝试使用 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”,但没有区别。谁能帮我这个?