Work with attachments

1-      Sending
// Define message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Hello JavaMail Attachment");

// Create the message part 
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText("Pardon Ideas");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

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

2-      Receiving
Getting attachments out of your messages is a little more involved then
sending them, as MIME has no simple notion of attachments. The content of
your message is a Multipart object when it has attachments. You then need to
process each Part, to get the main content and the attachment(s). Parts
marked with a disposition of Part.ATTACHMENT from part.getDisposition() are
clearly attachments. However, attachments can also come across with no
disposition (and a non-text MIME type) or a disposition of Part.INLINE.

Multipart mp = (Multipart)message.getContent();

for (int i=0, n=multipart.getCount(); i<n; i++) {
  Part part = multipart.getBodyPart(i));

  String disposition = part.getDisposition();

  if ((disposition != null) && 
      ((disposition.equals(Part.ATTACHMENT) || 
       (disposition.equals(Part.INLINE))) {
    saveFile(part.getFileName(), part.getInputStream());        // custom
save function
  }
}

2.1-    To cover all cases, handle when the disposition is null and get the
MIME type of the part to handle accordingly.

if (disposition == null) {
  // Check if plain
  MimeBodyPart mbp = (MimeBodyPart)part;
  if (mbp.isMimeType("text/plain")) {
    // Handle plain
  } else {
    // Special non-attachment cases here of image/gif, text/html, ...
  }
...
}

2.2     Alternative approach to the problem:

public void printParts(Part p) {
Object o = p.getContent();
if (o instanceof String) {              // it is a text/plain or a text/html
section, check ContentType. 
} else if (o instanceof Multipart) {    // it is a another multipart. Drill
down by recursing printParts()..
} else if (o instanceof InputStream) {  // it is an attachment!!!
}

- Example:

    public void doSomethingWithPart(Part p) throws Exception {
         Object o = p.getContent();
            if (o instanceof String) {
                // do something with the part
                p.setContent("Hello", "text/plain");
            } else if (o instanceof Multipart) {
Multipart mp = (Multipart) o;
                int count = mp.getCount();
                for (int i = 0; i < count; i++) {
                    doSomethingWithPart(mp.getBodyPart(i));
                }
               p.setContent(mp);        // in case there were changes;
           } else if (o instanceof InputStream) { }
    }

// message.saveChanges();

Br,

Isaac.

-----Original Message-----
From: Hoai [mailto:[EMAIL PROTECTED] 
Sent: jueves, 04 de noviembre de 2004 20:56
To: [EMAIL PROTECTED]
Subject: How to retrieve body's message

Hi all,

I am trying to write a Matcher to detect incoming email.  The matcher will
return an email address if the content (body) of email contains strings
like: abc=testing

If there is no attachment in the email, I am able to extract the content of
email using method .getContent().

But it does not work if email does have attachment.

Could you point me to the right direction of how to accomplish this?

Thank you.

Hoai



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.772 / Virus Database: 519 - Release Date: 10/1/2004



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to