Java Reference
In-Depth Information
The
getDisposition()
method returns a string indicating whether the content should
be presented in-line or as an attachment. The value returned should either be
null
(the
disposition is not known) or one of the two named constants,
Part.INLINE
or
Part.ATTACHMENT
:
public
static
final
String
ATTACHMENT
=
"attachment"
;
public
static
final
String
INLINE
=
"inline"
;
If the disposition is
Part.ATTACHMENT
,
getFileName()
should return the name of the
file to save the attachment in. Otherwise,
getFileName()
probably returns
null
. How‐
ever, some email clients do not properly set the Content-disposition header for attach‐
ments. Consequently, when receiving messages with attachments, you'll often get a
null
disposition but a non-null filename. In practice, it seems more reliable to assume that
any body part with a non-null filename is an attachment regardless of the Content-
disposition header, and any body part with no filename and no Content-disposition
header should be displayed in-line if possible. If it's not possible—for instance, if you
can't handle the MIME type—you can either ask the user for a filename or pick some
reasonable default, such as
attachment1.tif
.
Normally, the filename includes only the actual name of the file but not any of the
directories the file was in. It's up to the application receiving the message to decide where
to put the incoming file. For instance, Thunderbird generally stores attachments in the
Downloads folder inside the user's home directory or on the desktop. However, the user
has an option to pick a different location.
You need to be careful before using a filename that arrives in an email message. In
particular make sure you escape or remove any illegal characters or hierarchy delimiters
such as /. Different systems have different rules for file names so what's a legal file name
on Windows may not be on a Mac, and vice versa.
Furthermore, since it's not uncommon to receive multiple attachments with the same
name over time, check to see whether a file with the attached file's name already exists
before writing out the attachment. If a similarly named file does exist, you'll have to
rename the attachment in some reasonable fashion—for instance, by appending a 1 or
a 2 to it: e.g.,
vcard1.vcf
,
vcard2.vcf
, and so on.
The description, disposition, and filename attributes also have setter methods. However,
the size and line count attributes are determined by the content of the part rather than
a setter method:
public
void
setDisposition
(
String
disposition
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
void
setFileName
(
String
filename
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
void
setDescription
(
String
description
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException