在java中发送消息时是否有MessagingExceptionIOException?

时间:2011-01-06 07:14:07

标签: java javamail

我使用以下代码发送mail.Text消息发送工作正常但邮件附件不起作用它给Exception.How解决这个

  

javax.mail.MessagingException:发送消息时发生IOException;     嵌套异常是:       javax.activation.UnsupportedDataTypeException:没有MIME类型multipart / mixed的对象DCH;       边界= “---- = _ Part_0_10430987.1294298904906”       at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)       在javax.mail.Transport.send0(Transport.java:189)       在javax.mail.Transport.send(Transport.java:118)       在Gmailer.GMailSender.sendMailAttach(GMailSender.java:114)       在SendMail.main(SendMail.java:22)   引起:javax.activation.UnsupportedDataTypeException:没有MIME类型multipart / mixed的对象DCH;       边界= “---- = _ Part_0_10430987.1294298904906”       在javax.activation.ObjectDataContentHandler.writeTo(未知来源)       在javax.activation.DataHandler.writeTo(未知来源)       在javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)       在javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)       at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)       ......还有4个

带附件代码的邮件:

public synchronized void sendMailAttach(String subject, String body,
    String sender, String recipients)  {
try {
    MimeMessage message = new MimeMessage(session);

    message.setSender(new InternetAddress(sender));
    message.setSubject(subject);

    // Create the message part 
    BodyPart messageBodyPart = new MimeBodyPart();

    // Fill the message
    messageBodyPart.setText("hi Demo");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    String filename = "mail.txt";
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    message.setContent(multipart);

    if (recipients.indexOf(',') > 0)
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(recipients));
    else
        message.setRecipient(Message.RecipientType.TO,
                new InternetAddress(recipients));
    Transport.send(message);

}
catch (MessagingException e) {
    System.out.println("MessagingException" + e.getMessage());
}
catch (Exception e) {
    System.out.println("Mail Send Exception " + e.getMessage());
}
 }

文本邮件发送代码:

public synchronized void sendMail(String subject, String body,
    String sender, String recipients) throws Exception {
try {
    MimeMessage message = new MimeMessage(session);
    DataHandler handler = new DataHandler(new ByteArrayDataSource(
            body.getBytes(), "text/plain"));
    message.setSender(new InternetAddress(sender));
    message.setSubject(subject);
    message.setDataHandler(handler);
    if (recipients.indexOf(',') > 0)
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(recipients));
    else
        message.setRecipient(Message.RecipientType.TO,
                new InternetAddress(recipients));
    Transport.send(message);
} catch (Exception e) {

}
}

8 个答案:

答案 0 :(得分:4)

首先,您可以使用MimeBodyPart.attachFile()而不是自己处理DataSource / DataHandler代码,使代码更简洁。

其次,尝试使用适当的值设置附件中的Content-TypeContent-Disposition标题。 (attachFile默认情况下会为您设置Content-Disposition。)例如,

messageBodyPart = new MimeBodyPart();
messageBodyPart.attachFile(new File("mail.txt"));
messageBodyPart.setHeader("Content-Type", "text/plain; charset=\"us-ascii\"; name=\"mail.txt\"");


<小时/> 修改

在思考了一下之后,这必须是类加载的问题。请检查this other SO thread,看看它是否可以解决您的情况。 (一般问题:你的类路径中可能还有一个额外的activation.jar;还有一些其他的可能性也会导致它。)

答案 1 :(得分:1)

使用该代码放置该代码,然后检查它是否真的有用。 首先,导入该包

import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;

MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
        mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
        CommandMap.setDefaultCommandMap(mc);

答案 2 :(得分:0)

我认为您的程序无法找到mail.txt文件。 请给出正确/完整的路径。 异常堆栈跟踪可以提供更多的想法。 如果可能的话回复。 e.printStacktrace();

答案 3 :(得分:0)

这可能源于类加载问题(activation.jar的重复版本)。请参阅示例http://forum.springsource.org/archive/index.php/t-69180.html

或谷歌更多

答案 4 :(得分:0)

设置此值:

message.setContent(body, "text/html; charset=UTF-8");    
messageBodyPart.setContent(body, "text/html; charset=UTF-8");

同时检查文件路径,或者在日志中写下完整路径

答案 5 :(得分:0)

static {
     // add handlers for main MIME types
    MailcapCommandMap mcap = new MailcapCommandMap();
    mcap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    mcap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    mcap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    mcap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true");
    mcap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    CommandMap.setDefaultCommandMap(mcap);
}

答案 6 :(得分:0)

尝试定义文件的确切路径mail.txt 例如,如果它在C:中,那么继续并包括整个路径,如

 String filename = ("c:\\users\\mail.txt"); 

并且请注意,有时您可能会遇到("c:\users\mail.txt")有单个反斜杠的问题,所以为了安全起见,请将它们设为双反斜杠

同时始终确保使用.printStackTrace()方法,因为它可以帮助您确定问题的确切位置,以便在您搜索该问题时,您将能够准确了解您将要查找的内容对

答案 7 :(得分:0)

请参阅此链接:http://forum.spring.io/forum/osgi-related/dm-server-general/61205-problems-sending-mime-multipart-mails

它解释了问题的可能原因,并且通常建议解决问题。

我在jboss保险丝上使用apache camel时遇到了同样的问题。我不得不修改我的pom.xml ...请参阅下面的代码段:

    <!-- add a depency on javax.mail, in addition to the dependency on camel-mail -->

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-mail</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.1</version>
    </dependency>

    <!-- explicitly import the com.sun.mail.handlers package under the Import-Package section of the pom.xml -->

    <Import-Package>com.sun.mail.handlers, *</Import-Package>