2016-11-15 49 views
1

我送與HTML文本的電子郵件和附件,並得到錯誤:GWT Java電子郵件 - java.io.UnsupportedEncodingException:text/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不再有效。

+0

這Jakata常見的電子郵件API是爲了解決你的問題http://commons.apache.org/proper/commons-email/的另一種方式,這API也很好的郵件處理 –

回答

7

嘗試

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

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

嗨安德烈,第二個工程的一種享受。現在我遇到了附件名稱的問題。我會爲此提出一個新問題。 – Glyn