File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Not able to attch the document correctly in the email using java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Not able to attch the document correctly in the email using java code" Watch "Not able to attch the document correctly in the email using java code" New topic
Author

Not able to attch the document correctly in the email using java code

Foni kumar
Ranch Hand

Joined: Sep 19, 2007
Posts: 66
Hi Experts,

Please find below my custom code to send email with attachments. I am getting the email with the text message but the attachment isn't coming in that email message. The file is present in my ;local system in the "C:temp\excelFile.xls location as can be seen in the code. I am failing to understand why is the attachment failing to get attached to my email. I got no error message as well. Can anyone else suggest me if the code needs to be modified to get the attachment correct.

Could any one provide me any suggestions on this regard. I have made bold to the part of code snippet being used as an attachment.



Kind Regards
Deb..>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
The code doesn't seem to be attaching anything to the mail. It creates, but does not use, the Multipart object.


Ping & DNS - my free Android networking tools app
Foni kumar
Ranch Hand

Joined: Sep 19, 2007
Posts: 66
Ulf Dittmer wrote:The code doesn't seem to be attaching anything to the mail. It creates, but does not use, the Multipart object.


Hi,

Thanks for the reply..Could you explain it a bit further in the code snippet where I need to modify my code then...

Many Thanks !!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
There isn't much to explain; ask yourself this: what is the relationship between the "msg" and "multipart" objects, and how is that relationship expressed in the code?

If you need help with the JavaMail API, the JavaEnterpriseEditionFaq points to an extensive tutorial with many code examples.
Foni kumar
Ranch Hand

Joined: Sep 19, 2007
Posts: 66
Ulf Dittmer wrote:There isn't much to explain; ask yourself this: what is the relationship between the "msg" and "multipart" objects, and how is that relationship expressed in the code?

If you need help with the JavaMail API, the JavaEnterpriseEditionFaq points to an extensive tutorial with many code examples.


I asked myself the question and got an approach




The attachment is now working in my mail application but upon downloading the contents are not seen in the excel sheet,this is the same excel sheet as present in my local folder. Any suggestion , am I still missing something on my code part...

why are the contents not copied and when I ope excel sheet from the mail attachment it says the excel has read only access (but in actually it has write access thats not an issue at all)..

Kindly suggest why is the content not getting copied in the attachment.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
but upon downloading the contents are not seen in the excel sheet,this is the same excel sheet as present in my local folder

I'm confused. At the same time, the sheet has no content, and yet it is identical to the original file?
Foni kumar
Ranch Hand

Joined: Sep 19, 2007
Posts: 66
Ulf Dittmer wrote:
but upon downloading the contents are not seen in the excel sheet,this is the same excel sheet as present in my local folder

I'm confused. At the same time, the sheet has no content, and yet it is identical to the original file?



Yes , it is the same excel sheet which I have present in my local system and now when I send it through automated mail custom code, I see the same file in the attachment but upon downloading the content and opening the file I got the below error "The file cannot be accessed. The file may be read only or you are trying to access from a read only location" .. I changed the location of the downloaded file it is of no help because my original file size is around 20KB and the downloaded version is 8kb. It shows the contents are getting lost somewhere or may be not getting saved during this transaction...

What can be the cause ,is there also some commands I need to explore to save the contents of the attachment during the transaction of the whole process or so, or am i missing something in my code.



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
It's hard to say what's going on without seeing the relevant code section. The code excerpt you posted seems a bit odd, e.g., you still don't seem to make use of the multipart object.

The JavaMail tutorial at java.sun.com doesn't seem to be accessible at the moment, so I've put it up here: http://www.coderanch.com/how-to/content/Fundamentals-of-the-JavaMail-API.html. It has a full example of how to send attachments.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Ulf Dittmer wrote:The JavaMail tutorial at java.sun.com doesn't seem to be accessible at the moment

I could be wrong, but I think that page has been replaced with the JAVAMAIL API FAQ.

edit The most specific part for this problem appears to be http://www.oracle.com/technetwork/java/faq-135477.html#readofficedoc


luck, db
There are no new questions, but there may be new answers.
Foni kumar
Ranch Hand

Joined: Sep 19, 2007
Posts: 66
Darryl Burke wrote:
Ulf Dittmer wrote:The JavaMail tutorial at java.sun.com doesn't seem to be accessible at the moment

I could be wrong, but I think that page has been replaced with the JAVAMAIL API FAQ.

edit The most specific part for this problem appears to be http://www.oracle.com/technetwork/java/faq-135477.html#readofficedoc



Thanks very much Darryl for the great answer, i got my problem solved ;) the reason was I had not committed my changes ..I referre3d your API documentations and got the relevant answer. We shoudl use the method named

msg.saveChanges()
Here msg is an instance of Mime Message...

Thanks a ton! you made my day man!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
Darryl Burke wrote:
The JavaMail tutorial at java.sun.com doesn't seem to be accessible at the moment

I could be wrong, but I think that page has been replaced with the JAVAMAIL API FAQ.

No, that's the JavaMail FAQ. The jGuru tutorial with all the code examples that used to be on java.sun.com doesn't seem to be available anywhere else (apart from the place I linked to in my previous post).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Too difficult a question for “beginning”. Moving discussion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to attch the document correctly in the email using java code