0

我正在尝试将 Microsoft Word 文档的内容从 Java 代码放入电子邮件正文中,但出现以下错误:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/msword
    at co.kush.DemoEmail.main(DemoEmail.java:134)
Caused by: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/msword
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625)
    at co.kush.DemoEmail.main(DemoEmail.java:128)
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/msword
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:896)
    at javax.activation.DataHandler.writeTo(DataHandler.java:317)
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)

参考我的代码:

package co.kush;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.CharacterRun;


/**
* @author Kush 
* 
*/
public class DemoEmail {
 public static void main(String[] args) throws FileNotFoundException, IOException {

        Properties prop = new Properties();
        String Filepath = "Email.Properties";
        FileInputStream propFile = new FileInputStream(Filepath);
        prop.load(propFile);
        String timeStamp = new SimpleDateFormat("M/dd @ HH:mm:ss").format(Calendar.getInstance().getTime());

        // Fetching values from property file
        String sender = prop.getProperty("sender");
        String recevier = prop.getProperty("recevier");
        String password = prop.getProperty("password");
        String proxyHost = prop.getProperty("proxyHost");
        String proxyPort = prop.getProperty("proxyPort");
        String smtpHost = prop.getProperty("smtpHost");
        String smtpPort = prop.getProperty("smtpPort");
        String attachFilePath = prop.getProperty("attachFilePath");
        String mailSubject=prop.getProperty("mailSubject")+"  on " + timeStamp;
        String mailBody = prop.getProperty("mailBody");

        Properties sys_prop = System.getProperties();

        // Setting values for sending Email
        sys_prop.put("mail.smtp.starttls.enable", "true");
        sys_prop.put("mail.transport.protocol", "SMTP");
        sys_prop.put("mail.smtp.host", smtpHost);
        sys_prop.put("mail.smtp.auth", "true");
        sys_prop.put("mail.smtp.port", smtpPort);
        sys_prop.put("mail.smtp.debug", "true");
        sys_prop.put("mail.smtp.socketFactory.port", smtpPort);
        sys_prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        sys_prop.put("mail.smtp.socketFactory.fallback", "false");
        sys_prop.put("proxySet", "true");
        sys_prop.put("proxyPort", proxyPort);
        sys_prop.put("socksProxyHost", proxyHost);
        sys_prop.put("proxyHost", proxyHost);

        Session session = Session.getDefaultInstance(sys_prop, null);

        try {
            //Adding Sender/Receiver address and Subject Line
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(sender));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recevier));
            msg.setSubject(mailSubject);            

            MimeBodyPart messageBodyPart=new MimeBodyPart();
            Multipart multipart = new MimeMultipart();

            //Reading Word Document
              HWPFDocument doc = new HWPFDocument(new FileInputStream("<AbsolutePathWordDoc\\<WordDocFile>.doc"));
                WordExtractor we = new WordExtractor(doc);
                String[] paragraphs = we.getParagraphText();
                   for (String para : paragraphs) {
                    //Putting word document to email Body
                    messageBodyPart.setContent(paragraphs, "application/msword");
                    multipart.addBodyPart(messageBodyPart);
                }


            //Adding attachment and Body content to MIME msg object
            msg.setContent(multipart);

            //Sending the message
            Transport transport = session.getTransport("smtp");
            transport.connect(smtpHost, sender, password);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

            System.out.println("Mail sent succesfully!");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        } finally {
        //Removing System properties
            sys_prop.remove("proxySet");
            sys_prop.remove("proxyHost");
            sys_prop.remove("proxyPort");
            sys_prop.remove("socksProxyHost");
            sys_prop.remove("mail.smtp.socketFactory.class");
            sys_prop.remove("mail.smtp.socketFactory.fallback");
            sys_prop.remove("mail.smtp.socketFactory.port");

        }

}

}

属性文件“Email.Properties”:

sender=helloqa86@gmail.com
recevier=k.b@xx.com
password=*****
proxyHost=web-proxy.**.**.com
proxyPort= 8080
smtpHost=smtp.gmail.com
smtpPort=465

从错误中我可以理解问题出在这部分 messageBodyPart.setContent(paragraphs, "application/msword"); 我在其中将 word 文档内容添加到 MIME 消息正文部分。

我尝试了其他几种方法来实现这一点,但无法做到。请提出一种方法来实现这一目标?

4

1 回答 1

0

我解决了这个问题。实际上,我必须在我试图从 word 文档中读取的电子邮件正文中放置不同的字体和字体大小。为了实现这一点,我改变了我的方法,所以我嵌入的不是 word 文档我的内容转换成非常简单的 html 标签 :),请参考下面的代码行,它将替换 word 文档部分

String htmltext = "+ 不同的字体大小

+不同的字体大小

+ "; messageBodyPart.setContent(htmltext, "text/html");

在我研究期间,我的一位朋友建议我遵循第三方 API,这可能对任何人都有帮助

http://www.oracle.com/technetwork/java/javamail/third-party-136965.html

https://github.com/bbottema/simple-java-mail/

于 2017-02-21T06:38:05.333 回答