Java Mail Question : Attachement Problems

L

Lee

Hi there,

This looks like a frequently asked question, but none of the answers I
found with a Google search seem to help. I have a class that send email
messages, but when I send attachements I encounter problems. Plain text
files just appear in the message body, where as image files appear as
"gobbeldygook" (character encoding/whatever).

Does anyone use javax.mail frequently? Can you enlighten me as to what
my problem is? Snippets below:

-------------------------

msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setRecipients(MimeMessage.RecipientType.TO, addressList);

MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(messageBody, "us-ascii");
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
if...
{
for...
{
MimeBodyPart mbp2 = new MimeBodyPart();
ataSource source = new FileDataSource(attArr);
mbp2.setDataHandler(new DataHandler(source));
mbp2.setFileName(attArr);
mp.addBodyPart(mbp2);
}
}

msg.setContent(mp);
----------------------------
 
B

bm

Do you think the problem may reside in the recieving end
while processing or getting the attachments? May be the
disposition of the attachments are set to inline? What do
you think?
 
G

GaryM

Lee said:
This looks like a frequently asked question, but none of the
answers I found with a Google search seem to help. I have a class
that send email messages, but when I send attachements I encounter
problems. Plain text files just appear in the message body, where
as image files appear as "gobbeldygook" (character
encoding/whatever).

Lee, I am not seeing you set the mime type of the attachments? Have
you set your .mime.types file? If not perhaps you are doing it
programmaticly and in code you did not include?

Here's an example I use for images. You can take pieces from it as
you need:

// Get Image
MimeBodyPart getImagePart() {

MimeBodyPart imgPart = new MimeBodyPart();
String imgFile = "pathtomyimage";
try {

MimetypesFileTypeMap mftm = new MimetypesFileTypeMap();
mftm.addMimeTypes("image/jpeg");
FileDataSource imgFds=new FileDataSource(imgFile);
imgFds.setFileTypeMap(mftm);
imgPart.setDataHandler(new DataHandler(imgFds));

// If you have lots of images in the html you need to set
// this uniquely for each one (i think it is not generated)
imgPart.setContentID ("image1");

} catch (Exception e) {
System.out.println("Error creating image attachment");
System.exit (1);
}

return imgPart;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top