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
This may stem from class loading issues (duplicat versions of activation.jar). See for example http://forum.springsource.org/archive/index.php/t-69180.html
or google some more
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\"");
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.)