MessagingExceptionIOException while sending message in java?

前端 未结 8 942
说谎
说谎 2021-01-19 02:41

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

8条回答
  •  执笔经年
    2021-01-19 03:30

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

    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\"");
    



    EDIT:

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

提交回复
热议问题