在java中发送消息时是否有MessagingExceptionIOException? [英] MessagingExceptionIOException while sending message in java?

查看:3284
本文介绍了在java中发送消息时是否有MessagingExceptionIOException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the following code to send mail.Text message sending is working fine but Mail with attachment is not working it gives the Exception.How to solve this


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

javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_10430987.1294298904906" at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676) at javax.mail.Transport.send0(Transport.java:189) at javax.mail.Transport.send(Transport.java:118) at Gmailer.GMailSender.sendMailAttach(GMailSender.java:114) at SendMail.main(SendMail.java:22) Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_10430987.1294298904906" at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) at javax.activation.DataHandler.writeTo(Unknown Source) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636) ... 4 more

带附件代码的邮件:

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) {

}
}


推荐答案

首先,您可以使用 MimeBodyPart.attachFile()而不是纠缠 DataSource / DataHandler 自己编码。

Firstly, you could make your code a little more concise by using MimeBodyPart.attachFile() instead of wrangling the DataSource/DataHandler code yourself.

其次,尝试设置附件部分上的 Content-Type Content-Disposition 标头,并带有适当的值。 (默认情况下, attachFile 会为您设置Content-Disposition。)例如,

Secondly, try setting the Content-Type and Content-Disposition headers on your attachment part with appropriate values. (attachFile will set the Content-Disposition for you by default.) For instance,

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




编辑:

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

After thinking a bit, this has to be something amiss with class loading. Please check this other SO thread to see if it remedies your situation. (General issue: Probably an extra activation.jar in your classpath; a few other possibilities also thought to cause it.)

这篇关于在java中发送消息时是否有MessagingExceptionIOException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆