Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Issue with procesing email with attachments via JavaMail

843830May 29 2003 — edited Jun 3 2003
Hello all,

I am trying to process emails, specifically emails with attachments. The code is 99% like the sample/demo code.

The issue is that everything I am reading says its content type is "text/plain" and when I call getContent() I am returned a string that contains the entire message, including the attachment.

What I am expecting is a mutlipart email to be returned.

Any ideas would be greatly appreciated.

P.S. other email readers process these messages as multipart (Eudora and a webmail reader).


here is the code for what I am doing
(I tried to add html tags to keep the formating, they became part of the message)

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;


public class MessageHandler extends Thread {

static String host = "localhost";
static String user = "test-user";
static String pword = "11111111";
static String from = "rloek@group1internet.com";
static String to = "test-user@localhost";

/**
* ***********************************************************
*/
public MessageHandler() {
setName("MsgHandler");
Properties props = System.getProperties();
// create some properties and get the default Session
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
start();
}

/**
* ***********************************************************
*/
synchronized
static
public void sendMessage(String to, String from, String subject, String filename) {

Properties props = System.getProperties();

String msgText1 = "Sending a file.\n";

Session session = Session.getDefaultInstance(props, new xAuthenticator());
session.setDebug(true);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);

// create and fill the first message part
BodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);

// create the second message part
BodyPart mbp2 = new MimeBodyPart();

// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());

// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp2);
mp.addBodyPart(mbp1);

// add the Multipart to the message
msg.setContent(mp);

// set the Date: header
msg.setSentDate(new Date());
msg.saveChanges() ;

// send the message
Transport.send(msg);

} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}

/**
* ***********************************************************
*/
public void run() {
// look for new messages
int sleepTime = 1000*5;
// need to get email server information

while (true) {
// check for new email from the server
Properties props = System.getProperties();
Store store = null;
Session session = Session.getDefaultInstance(props, new xAuthenticator());
try {
store = session.getStore("pop3");
store.connect(host, user, pword);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message[] message = folder.getMessages();
for (int i = 0; i < message.length; i++) {
Message msg = message;
try {
String t = msg.getContentType();
Object ct = msg.getContent();
if (msg.isMimeType("multipart/*")) {
Multipart mp = (Multipart)msg.getContent();
for (int j = 0, n = mp.getCount(); i < n; i++) {
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
if ((null != disposition ) &&
disposition.equals(Part.ATTACHMENT) ||
disposition.equals(Part.INLINE)) {
saveFile(part.getFileName(), part.getInputStream());
}
}
}
else {
System.out.println("Dumping message:");
message[i].writeTo(System.out);
}
}
catch (Exception e) {e.printStackTrace();}
// delete the message
// message[i].setFlag(Flags.Flag.DELETED, true);
}
folder.close(true);
store.close();

}
catch (NoSuchProviderException e) {e.printStackTrace();}
catch (MessagingException e){e.printStackTrace();}

try {
sleep(sleepTime);
}
catch (InterruptedException e) {}
}
}

void saveFile(String filename, InputStream is) {
File f = new File(filename);
try {
OutputStream os =
new BufferedOutputStream(new FileOutputStream(f));
int c;
while ((c = is.read()) != -1)
os.write(c);
os.close();
} catch (IOException ex) {
System.out.println("Failed to save attachment: " + ex);
}
}

static
class xAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pword);
}
}

static
public void main(String[] args) {
String path = System.getProperties().getProperty(net.intershield.DNSMagic.CONFIG_DIR);

MessageHandler smh = new MessageHandler();

smh.sendMessage(to, from, "test message", "c:/temp/test.txt");
smh.sendMessage(to, from, "test message", "c:/temp/test.bin");
}
}
</pre>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 1 2003
Added on May 29 2003
4 comments
407 views