Sunteți pe pagina 1din 5

Sending email using JAVA

Multipurpose Internet Mail Extensions (MIME) is an Internet standard that


extends the format of e-mail to support:

• Text in character sets other than ASCII


• Non-text attachments
• Message bodies with multiple parts
• Header information in non-ASCII character sets

MIME's use, however, has grown beyond describing the content of e-mail to
describing content type in general, including for the web (see Internet media type).

Virtually all human-written Internet e-mail and a fairly large proportion of automated
e-mail is transmitted via SMTP in MIME format. Internet e-mail is so closely
associated with the SMTP and MIME standards that it is sometimes called
SMTP/MIME e-mail.

javax.mail
public interface Part

The Part interface is the common base interface for Messages and BodyParts.

Part consists of a set of attributes and a "Content".

Attributes:

The JavaMail API defines a set of standard Part attributes that are considered to be
common to most existing Mail systems. These attributes have their own settor and
gettor methods. Mail systems may support other Part attributes as well, these are
represented as name-value pairs where both the name and value are Strings.

Content:

The data type of the "content" is returned by the getContentType() method. The
MIME typing system is used to name data types.

The "content" of a Part is available in various formats:

• As a DataHandler - using the getDataHandler() method. The "content" of a


Part is also available through a javax.activation.DataHandler object. The
DataHandler object allows clients to discover the operations available on the
content, and to instantiate the appropriate component to perform those
operations.
• As an input stream - using the getInputStream() method. Any mail-specific
encodings are decoded before this stream is returned.
• As a Java object - using the getContent() method. This method returns the
"content" as a Java object. The returned object is of course dependent on the
content itself. In particular, a "multipart" Part's content is always a Multipart
or subclass thereof. That is, getContent() on a "multipart" type Part will
always return a Multipart (or subclass) object.

public interface MimePart


extends Part

• The MimePart interface models an Entity as defined by MIME (RFC2045,


Section 2.4).
• MimePart extends the Part interface to add additional RFC822 and MIME
specific semantics and attributes. It provides the base interface for the
MimeMessage and MimeBodyPart classes

public class MimeBodyPart


extends BodyPart
implements MimePart

This class represents a MIME body part. It implements the BodyPart abstract class
and the MimePart interface. MimeBodyParts are contained in MimeMultipart
objects.

MimeBodyPart uses the InternetHeaders class to parse and store the headers of that
body part.

public abstract class BodyPart


extends Object
implements Part

This class models a Part that is contained within a Multipart. This is an abstract class.
Subclasses provide actual implementations.

BodyPart implements the Part interface. Thus, it contains a set of attributes and a
"content".
public abstract class Multipart
extends Object

Multipart is a container that holds multiple body parts. Multipart provides methods to
retrieve and set its subparts.

Multipart also acts as the base class for the content object returned by most Multipart
DataContentHandlers. For example, invoking getContent() on a DataHandler whose
source is a "multipart/signed" data source may return an appropriate subclass of
Multipart.

Some messaging systems provide different subtypes of Multiparts. For example,


MIME specifies a set of subtypes that include "alternative", "mixed", "related",
"parallel", "signed", etc.

Multipart is an abstract class. Subclasses provide actual implementations.

public abstract class Message


extends Object
implements Part

• This class models an email message. This is an abstract class. Subclasses


provide actual implementations.
• Message implements the Part interface. Message contains a set of attributes
and a "content". Messages within a folder also have a set of flags that describe
its state within the folder.
• Message defines some new attributes in addition to those defined in the Part
interface. These attributes specify meta-data for the message - i.e., addressing
and descriptive information about the message.
• Message objects are obtained either from a Folder or by constructing a new
Message object of the appropriate subclass. Messages that have been received
are normally retrieved from a folder named "INBOX".
• A Message object obtained from a folder is just a lightweight reference to the
actual message. The Message is 'lazily' filled up (on demand) when each item
is requested from the message. Note that certain folder implementations may
return Message objects that are pre-filled with certain user-specified items. To
send a message, an appropriate subclass of Message (e.g., MimeMessage) is
instantiated, the attributes and content are filled in, and the message is sent
using the Transport.send method.

public class MimeMessage


extends Message
implements MimePart

This class represents a MIME style email message. It implements the Message
abstract class and the MimePart interface.

Clients wanting to create new MIME style messages will instantiate an empty
MimeMessage object and then fill it with appropriate attributes and content.

Service providers that implement MIME compliant backend stores may want to
subclass MimeMessage and override certain methods to provide specific
implementations. The simplest case is probably a provider that generates a MIME
style input stream and leaves the parsing of the stream to this class.

MimeMessage uses the InternetHeaders class to parse and store the top level RFC
822 headers of a message.

The mail.mime.address.strict session property controls the parsing of address


headers. By default, strict parsing of address headers is done. If this property is set to
"false", strict parsing is not done and many illegal addresses that sometimes occur in
real messages are allowed. See the InternetAddress class for details.

public static class Message.RecipientType


extends Object
implements Serializable

This inner class defines the types of recipients allowed by the Message class. The
currently defined types are TO, CC and BCC. Note that this class only has a protected
constructor, thereby restricting new Recipient types to either this class or subclasses.
This effectively implements an enumeration of the allowed Recipient types. The
following code sample shows how to use this class to obtain the "TO" recipients from
a message.

Message msg = folder.getMessages(1);


Address[] a = m.getRecipients(Message.RecipientType.TO);

S-ar putea să vă placă și