【问题标题】:GWT Java email - java.io.UnsupportedEncodingException: text/htmlGWT Java 电子邮件 - java.io.UnsupportedEncodingException: text/html
【发布时间】:2017-03-28 21:36:50
【问题描述】:

我正在发送带有 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();
    }       
}

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

【问题讨论】:

标签: java email html-email


【解决方案1】:

试试

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

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

【讨论】:

  • 嗨,Andrei,第二个很好。现在我对附件名称有疑问。我会为此提出一个新问题。
  • 这个对我有用:MimeBodyPart messagePart = new MimeBodyPart(); messagePart.setText(html,"UTF-8","html");
【解决方案2】:

这对我有用 java >= 8:

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

使用 setContent 代替 setText

【讨论】:

    猜你喜欢
    • 2013-11-05
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 2013-05-26
    • 2018-12-05
    • 2015-05-13
    相关资源
    最近更新 更多