GWT Java 电子邮件 - java.io.UnsupportedEncodingException: text/html

GWT Java email - java.io.UnsupportedEncodingException: text/html

我正在发送一封包含 html 文本和附件的电子邮件,但收到错误消息:

java.io.UnsupportedEncodingException: text/html

密码是:

public void emailMessage(String emailSubject, String message, String emailaddress, String imagePath) {  
    //Send an email
    try {

        //Send an email
        SimpleEmail email = new SimpleEmail();
        email.setHostName("mail.org");
        email.setSmtpPort(25); //No authentication required
        email.setFrom("address.org");
        email.addTo(emailaddress);
        email.setSubject(emailSubject);
        email.setCharset("utf-8");

        // Set the email message text.
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(message, "text/html");

        // Set the email attachment file
        FileDataSource fileDataSource = new FileDataSource(imagePath);

        MimeBodyPart attachmentPart = new MimeBodyPart();
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(fileDataSource.getName());

        // Create Multipart E-Mail.
        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);

        email.setContent(multipart);

        //Send the email
        email.send();

    } catch (EmailException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

最初我发送的是一封没有附件的电子邮件,但效果很好。然后我为附件添加了多部分,text/html 不再有效。

尝试

textPart.setText(text, "utf-8" );

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

这对我有用 java >= 8:

MimeMessage msg = new MimeMessage(session)
msg.setContent(content, "text/html")

使用 setContent 代替 setText