Solved

Viewing .gif, .lwp, .zip, .123 using javamail

Posted on 2004-08-24
17
553 Views
Last Modified: 2013-11-24
I am sending mail via javamail, but when I try to save certain files to my system and view them the files are corrupt.  I am not able to access .gif, .lwp, .123, .zip, or .exe.  I am setting the content-type based on the file extension.  Any ideas?

int i = attachmentfile.lastIndexOf(".");
String s = attachmentfile.substring(i+1);
s = s.trim();
String type = null;
      
if(s.equalsIgnoreCase("zip")){
type="application/x-bolero-doc";
}
else if(s.equalsIgnoreCase("doc")||(s.equalsIgnoreCase("lwp"))){
MimetypesFileTypeMap map = new MimetypesFileTypeMap();
map.addMimeTypes("application/msword");
type="application/msword";
mbp2.setHeader("Content-Type", "application/msword");
}
else if(s.equalsIgnoreCase("txt")||(s.equalsIgnoreCase("text"))){
type="text/plain";
}
else if(s.equalsIgnoreCase("htm")||s.equalsIgnoreCase("html")){
type="text/html";
}
else if(s.equalsIgnoreCase("xls")){
type="application/excel";
}
else if(s.equalsIgnoreCase("pdf")){
MimetypesFileTypeMap map = new MimetypesFileTypeMap();
map.addMimeTypes("application/pdf");
type="application/pdf";
mbp2.setHeader("Content-Type", "application/pdf");
}
else if(s.equalsIgnoreCase("gif")){
type="image/gif";
}
else if(s.equalsIgnoreCase("jpeg")||(s.equalsIgnoreCase("jpg")||(s.equalsIgnoreCase("jpe")))){
type="image/jpeg";
}
else if(s.equalsIgnoreCase("mpeg")||(s.equalsIgnoreCase("mpg"))){
type="audio/x-mpeg";
}
else if(s.equalsIgnoreCase("wav")){
type="audio/x-wav";
}
else {
type="application/octet-stream";
}
                              
0
Comment
Question by:rws111
  • 8
  • 6
  • 3
17 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 11881827
If you use for all:
type="application/octet-stream";
are there any problems?

Giant.
0
 

Author Comment

by:rws111
ID: 11882126
Yes, I began with setting the type for everything equal to "application/octet-stream".  Then changed it to set type based on file extension, but no progress.....
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11882196
mmmhm.

and trying to use
content="text/html
even for gif (for example).
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 12

Expert Comment

by:Giant2
ID: 11882218
Do you have seen here?
http://www.faqs.org/rfcs/rfc1341.html
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11882255
I hope you set the MimeType with the type selected after all the if else else...else.

Isn't it?
0
 

Author Comment

by:rws111
ID: 11882407
this is the remainder of the code where attachmentBody is the bytes for the file contents - don't believe i'm setting the MimeType - where/how do i do that?
ByteArrayDataSource bads =
new ByteArrayDataSource(
attachmentBody,
type
);
mbp2.setDataHandler(new DataHandler(bads));
int idx = attachmentfile.lastIndexOf("\\"); //TODO both slashes
mbp2.setFileName(attachmentfile.substring(idx));
0
 
LVL 12

Accepted Solution

by:
Giant2 earned 250 total points
ID: 11882661
attachmentBody could be iso-8859-1, UTF-8, or other
then you could set even:
mbp2.setHeader(...)
0
 

Author Comment

by:rws111
ID: 11884503
setting the header did not help ....
mbp2.setHeader("Content-Transfer-Encoding", "base64" );
mbp2.setHeader("Content-Transfer-Encoding", "UTF-8" );
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 11887177
are you sure its not related to the actual attachment contents you are sending?
0
 

Author Comment

by:rws111
ID: 11887211
actually i am using a ByteArrayDataSource and in my InputStream method i had a statement to remove the final CR/LF - well that worked for some, but corrupted others - after commenting out that statement it all worked fine.  i also set the default content-type to octet-stream. thanks alot for the help!

public InputStream getInputStream() throws IOException {
if (data == null) throw new IOException("no data");
return new ByteArrayInputStream(data);
//return new ByteArrayInputStream(data,0,data.length - 2); //remove the final CR/LF
}
0
 

Author Comment

by:rws111
ID: 11887216
requesting close of this question
0
 
LVL 92

Expert Comment

by:objects
ID: 11887255
> requesting close of this question

why?
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11889487
> requesting close of this question

So do you solved all?
0
 

Author Comment

by:rws111
ID: 11905421
yes it has been resolved, the solution is noted in my comment on 08/24/2004 02:34PM PDT
0
 
LVL 92

Expert Comment

by:objects
ID: 11907537
and didn't I suggest above to check the sending of the attachment :)
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11910129
>yes it has been resolved

So I suggest 2 ways:

Now you want to delete this post? [send a zero point message to the community support:
http://www.experts-exchange.com/Community_Support/    asking for deleting this post and refund points]

Now you want to close this post? [assign the points to Expert/s help you]

Bye, Giant.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11928859
Thanks.
:)
Happy to help you.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap not working 8 58
How  can  i  resolve  HTTP Status 404 -? 8 44
jboss 7.1 start up error 1 40
jsp error 6 26
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question