Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get Attachment in Java Send email code?

 
Miki Draka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm working on my project in Java to send emails from database with attachments. My code send emails plus all Body part but I'm not getting any attachments which are pdf format. I haven't work before with
javax.mail.api and I'm not familiar with all syntax that I need to fix my code. Here is the code that I use:



If anyone can tell me what I'm missing in my code please let me know.
 
salvin francis
Bartender
Posts: 1407
18
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this tutorial might help ?

http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm

As I understand it correctly, you need to add body part to your message
 
Miki Draka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed tutorial but still something is wrong with my code. I still cannot get my .pdf file attached in email, in other hand .txt file are attached and worked fine.
Here is the code that I use now:

 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miki Draka wrote:

That last line is wrong. The multipart's content type is not "application/pdf". You should just call message.setContent(multipart);. The application/pdf should go on the messageBodyPart instead. The DataHandler will take care of that, provided that it can figure out the correct mime type for the source (based on its filename).
 
Miki Draka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Miki Draka wrote:

That last line is wrong. The multipart's content type is not "application/pdf". You should just call message.setContent(multipart);. The application/pdf should go on the messageBodyPart instead. The DataHandler will take care of that, provided that it can figure out the correct mime type for the source (based on its filename).


Miki wrote:
Thank you. That caused my code to crash.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean my suggestion caused a "crash"? If so, please post the stack trace (because I assume that "crash" means exception).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!